Anthropic SDK
Call Claude models through HopBase with the Anthropic SDK.
Keep the Anthropic Messages API structure and replace only the Base URL, Claude plan key, and model ID.
| Item | Value |
|---|---|
| Base URL | https://api.hop-base.com without /v1 |
| Key environment variable | HOPBASE_CLAUDE_API_KEY |
| Protocol | Anthropic Messages: POST /v1/messages; also POST /v1/messages/count_tokens |
| Model ID | Use an ID returned by GET /v1/models for the Claude plan key |
Install and send a minimal request
python -m pip install anthropicimport os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.hop-base.com",
api_key=os.environ["HOPBASE_CLAUDE_API_KEY"],
)
msg = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
)
print(msg.content[0].text)Use a key from the Claude Max (Official, Full Quota) or Claude Max (Official, Full Quota) - Limited-Time Offer group. The Claude Max (ccmax) group is for the Claude Code client only; SDK / API calls are rejected.
Do not configure a Claude plan key as OpenAI Compatible. Keep the SDK's native Anthropic authentication and Messages API request structure. Calling an OpenAI-protocol endpoint with a Claude key returns 404.
Read the charge for a request
A billed non-streaming request returns the USD amount actually deducted from your balance in the X-HopBase-Cost-USD response header, the same charge in CNY in X-HopBase-Cost, and the currency of X-HopBase-Cost (always CNY) in X-HopBase-Currency. The response also carries the same cost, cost_usd, and currency in usage, and the SDK keeps them as extra fields. Streaming responses carry none of these, and the usage in message_delta is unchanged; check the charge under Usage in the console. See Charges for the full rules.
raw = client.messages.with_raw_response.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Reply only with ok"}],
)
print(raw.headers.get("x-hopbase-cost"), raw.headers.get("x-hopbase-cost-usd"), raw.headers.get("x-hopbase-currency"))
msg = raw.parse() # Same return value as without with_raw_response
print(msg.usage.cost, msg.usage.cost_usd, msg.usage.currency) # Extra fields, same values as the headersSupported request parameters
This table lists only what the gateway fills in or handles specially. Other Messages API fields (system, tools, thinking, temperature, and so on) are forwarded unchanged and follow Anthropic's official limits. Context and output limits per model are in the model list.
| Parameter / header | Required | Type & limits | Default | Description |
|---|---|---|---|---|
x-api-key or Authorization: Bearer | Required | A Claude plan key starting with sk- | — | Both headers are accepted; a missing key returns 401 missing_api_key, a malformed one 401 invalid_api_key |
model | Required | string, a model ID in the current key's group | — | A model outside the group returns 404 model_not_found |
max_tokens | Optional | integer; no gateway ceiling, the maximum follows the model's official limit | 4096 | Required by the official API; when omitted HopBase fills in 4096, so long output stops at 4096 tokens (stop_reason: "max_tokens"). Set it explicitly |
messages | Required | array in Anthropic's official format | — | Forwarded unchanged |
stream | Optional | boolean | false | true returns the Anthropic SSE event stream |
anthropic-version / anthropic-beta | Optional | string headers | — | SDKs add them automatically; forwarded unchanged |
The whole request body is capped at 60 MB; larger bodies return 413.
Next steps
- To use Claude in the terminal, see Claude Code
- For GPT models, use the OpenAI SDK
- When something fails, check Troubleshooting