Anthropic SDK
Use Anthropic's official Python or TypeScript SDK to call the AGTCloud Messages compatible interface.
The official Anthropic SDK allows AGTCloud to call the Anthropic Messages compatible entry via custom base URLs. This tutorial provides examples of installing Python and TypeScript, as well as non-streaming requests and streaming output.
This page only covers Messages calls. Whether batch files, files, management interfaces, and other Anthropic platform resources are available should be determined by the AGTCloud API Reference.
Preparation
Before you start, please confirm that you have:
- Create the AGTCloud API Key on the API Keys page;
- Copy the full model ID supporting Anthropic Messages from Model Marketplace;
- Prepare a server runtime with Python
3.9+or Node.js20+.
This page uses MODEL_ID and your-api-key as placeholders. Do not store a real API Key in browser code, mobile app packages, or public repositories.
Install the SDK
python -m pip install anthropicIt is recommended to install the current stable version and fix the actual version through the project's dependency lock file.
Configure the API Key
Save the API Key to the server environment variable:
export AGTCLOUD_API_KEY="your-api-key"The examples read the credential from AGTCLOUD_API_KEY and pass it explicitly to the Anthropic SDK. The SDK sends the credential in the x-api-key header and automatically adds the anthropic-version header required by Messages requests.
Create a client and send a request
Use the API Origin shown on this page as the Base URL. Do not append /v1 or /v1/messages; the SDK adds the Messages path automatically.
import os
from anthropic import Anthropic
api_key = os.environ["AGTCLOUD_API_KEY"]
client = Anthropic(
api_key=api_key,
base_url="https://api.agtcloud.cn",
)
message = client.messages.create(
model="MODEL_ID",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Please introduce yourself in three sentences.",
}
],
)
for block in message.content:
if block.type == "text":
print(block.text)The content of Anthropic Messages is the array of content blocks. When reading text, check the content block type and do not assume the response always has only one text block. Tool calls, images, and other content types use different block structures.
Streaming output
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["AGTCLOUD_API_KEY"],
base_url="https://api.agtcloud.cn",
)
with client.messages.stream(
model="MODEL_ID",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Please briefly explain what vector retrieval is.",
}
],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Streaming Messages are returned via SSE events. Production environments should simultaneously handle connection interruptions, timeouts, rate limits, and situations where the full content is not received before the stream ends.
Frequently Asked Questions
| Issue | What to check |
|---|---|
401 or 403 | Confirm that AGTCLOUD_API_KEY is loaded, contains no extra spaces, and is still valid |
404 or an incorrect request path | Set base_url or baseURL to https://api.agtcloud.cn only; do not append /v1 or a full endpoint |
| The model is unavailable | Replace MODEL_ID with a model ID visible to the current API Key that supports Anthropic Messages |
| Request fields are not supported | Check the target model's capabilities and refer to the field definitions in the Anthropic Messages Reference |
| TypeScript refuses to run in the browser | The Anthropic SDK targets server environments by default; do not expose the API Key in browser code to bypass this restriction |
| Streaming content is empty | Check the event type and confirm that the code reads text_delta events or the SDK's text-stream helper |
This tutorial uses x-api-key
Pass the AGTCloud API Key through the SDK's api_key or apiKey parameter. The SDK sends it as x-api-key according to the Messages protocol. Do not add a second set of authentication headers to the same request.
Next
- See Anthropic Messages API Reference to learn about Content Blocks, tool calls, extended thinking, and full response fields.
- For existing original SDK projects, you can refer to Provider SDK Migration to replace configurations item by item and regress for testing.
- If the request fails, refer to Authentication and API Key and Error and Troubleshooting.