Using Ace Data Cloud with LangChain
LangChain's Python langchain-openai package provides ChatOpenAI, which can call chat models through Ace Data Cloud's OpenAI-compatible Chat Completions API. Integration only requires specifying a Token, model, and Base URL; there is no need to install an additional Ace Data Cloud LangChain package.
¶ Prepare a Token and dependencies
Use Python 3.10 or later. Sign in to the application list, open the General Application, and click the copy button to the right of API KEY.

If you need to restrict the scope of LangChain calls separately, you can create another Key under Manage Keys in the General Application. Check permissions, quotas, and balance before making calls.
Copy the raw Token without the Bearer prefix. A platform-... account management Token cannot replace the application key used to call models. Do not write the Token into code or commit it to a repository. You can first create a separate environment and then install the dependencies:
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install 'langchain-openai==1.7.0'
export ACEDATACLOUD_API_TOKEN='你的 Token'
In Windows PowerShell, use .venv\Scripts\Activate.ps1 to activate the environment and $env:ACEDATACLOUD_API_TOKEN='your Token' to set the key. The synchronous conversation below submits one potentially billable request; the streaming and tool examples must be run separately.
¶ Synchronous conversation
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4o-mini",
api_key=os.environ["ACEDATACLOUD_API_TOKEN"],
base_url="https://api.acedata.cloud/v1",
use_responses_api=False,
stream_usage=False,
max_retries=0,
timeout=60,
)
reply = llm.invoke("用一句话介绍 LangChain。")
print(reply.content)
print(reply.usage_metadata)
print(reply.response_metadata.get("id"))
The Base URL should end with /v1; ChatOpenAI automatically requests /v1/chat/completions and automatically adds the Bearer authorization header. use_responses_api=False ensures that Chat Completions is used; some model names may cause LangChain to automatically select a different API. The example uses gpt-4o-mini by default; for other models, refer to the model catalog and current pricing and your currently available permissions.
¶ Streaming output
for chunk in llm.stream("从一数到三。"):
if isinstance(chunk.content, str):
print(chunk.content, end="", flush=True)
print()
If the selected model supports OpenAI's stream_options.include_usage, you can change the initialization parameter to stream_usage=True to request token usage at the end of the stream. If the streaming response does not include usage, check the call records in the console.
¶ Tool calling
ChatOpenAI.bind_tools() can send standard OpenAI tool definitions to models that support tool calling. After the model returns a tool call, the application needs to execute the tool and then pass the result back to the model as a ToolMessage. See the LangChain example in the SDK repository for a complete runnable example. Its --mode tools option demonstrates a local addition tool and the complete loop; tool selection and the final answer each call the API once, for a total of two billable requests. Support for tool calling may vary between models.
¶ Usage, billing, and errors
reply.usage_metadata contains the token statistics in the response, not the Credits charged. reply.response_metadata["id"] is the API response ID, which can be referenced when troubleshooting; refer to usage history for the actual charges. The example sets max_retries=0 to prevent LangChain from automatically resubmitting potentially billable requests after a failure; when adding retries or Agent loops yourself, check usage for each call.
| Result | What to check |
|---|---|
| 401 | Whether the Token is valid and whether an extra Bearer prefix was pasted. |
| 403 | Check the balance, permissions, or content policy based on the error details. |
| 429 | A rate limit was encountered; wait before deciding whether to retry. |
| 404 | Check /v1 in the Base URL and the model ID. |
| 5xx / timeout | Check usage history before retrying a call with an uncertain result. |
This approach applies to standard OpenAI Chat Completions fields. LangChain's ChatOpenAI does not guarantee that service-specific response fields will be preserved; asynchronous tasks such as image, video, and music generation are also not called through this chat adapter. Refer to the LangChain ChatOpenAI documentation and the Chat Completions compatibility guide.
