Claude Agent SDK with a custom base URL: setup
Run the Claude Agent SDK on UplinkSwitch: the two environment variables, the env option in TypeScript and Python, a minimal query() agent, and model ids.
The Claude Agent SDK reaches UplinkSwitch through two environment variables, ANTHROPIC_BASE_URL="https://api.uplinkswitch.com/v1" and ANTHROPIC_API_KEY holding your gateway key, passed through the SDK's env option. The SDK has no base-URL client option of its own: it runs the Claude Code binary as a subprocess, and that binary sends each model call to the Anthropic Messages endpoint the gateway serves. This guide covers the install, both ways of passing the two variables, one minimal agent in TypeScript and Python, and what the gateway's Messages endpoint does and does not promise.
Install the package
The TypeScript package is @anthropic-ai/claude-agent-sdk and the Python package is claude-agent-sdk. Both bundle a native Claude Code binary, so most machines need no separate Claude Code install.
npm install @anthropic-ai/claude-agent-sdk
# or
pip install claude-agent-sdkOne exception from the SDK's own notes: an npm install that skips optional dependencies (npm ci --omit=optional) gets no binary. Reinstall without that flag, or install Claude Code yourself and set pathToClaudeCodeExecutable to it.
Point the subprocess at the gateway
Neither query() nor ClaudeSDKClient takes a baseUrl or base_url argument, because the agent's requests leave from that subprocess, not from your code; the override is the Claude Code variable ANTHROPIC_BASE_URL. Pair it with your gateway key in ANTHROPIC_API_KEY, which goes out as the x-api-key header the gateway reads:
export ANTHROPIC_BASE_URL="https://api.uplinkswitch.com/v1"
export ANTHROPIC_API_KEY="sk-uplink-live-..."Keep the /v1 on the end, the same form the live Claude Code guide publishes; the gateway accepts the Messages path the binary builds from it. If you would rather send the key as a Bearer value, ANTHROPIC_AUTH_TOKEN does that instead. Set one of the two, not both.
A minimal agent in TypeScript
query() returns an async iterator of messages. The sketch below names the model, pre-approves two read-only tools, caps the loop at five turns and passes the gateway variables through env:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Read package.json and list the scripts it defines.",
options: {
model: "claude-sonnet-5-5",
allowedTools: ["Read", "Glob"],
maxTurns: 5,
env: {
...process.env,
ANTHROPIC_BASE_URL: "https://api.uplinkswitch.com/v1",
ANTHROPIC_API_KEY: process.env.UPLINKSWITCH_API_KEY,
},
},
})) {
if (message.type === "assistant" && message.message?.content) {
for (const block of message.message.content) {
if ("text" in block) console.log(block.text);
}
} else if (message.type === "result") {
console.log(`Done: ${message.subtype}`);
}
}The spread of process.env matters. Per the SDK's TypeScript reference, env replaces the subprocess environment rather than merging into it, so leaving the spread out drops PATH and the binary fails to start.
The same agent in Python
import asyncio
import os
from claude_agent_sdk import query, ClaudeAgentOptions, AssistantMessage, ResultMessage
async def main():
options = ClaudeAgentOptions(
model="claude-sonnet-5-5",
allowed_tools=["Read", "Glob"],
max_turns=5,
env={
"ANTHROPIC_BASE_URL": "https://api.uplinkswitch.com/v1",
"ANTHROPIC_API_KEY": os.environ["UPLINKSWITCH_API_KEY"],
},
)
async for message in query(prompt="Read pyproject.toml and name its dependencies.", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if hasattr(block, "text"):
print(block.text)
elif isinstance(message, ResultMessage):
print(f"Done: {message.subtype}")
asyncio.run(main())Per the Python reference, env there is merged on top of the inherited environment, so no spread is needed there. allowed_tools auto-approves the listed tools; it does not stop the agent from asking for others, which fall through to permission_mode.
Which model ids to use
The model option takes the catalog id as written, with no vendor prefix. Claude Code also runs background work on its haiku alias, so set ANTHROPIC_DEFAULT_HAIKU_MODEL in the same env block to an id the catalog marks ok; left unset, that work goes out under Claude Code's built-in default id instead.
| Model id | Status on the 2026-11-23 catalog | Price per 1M, in / out | Context |
|---|---|---|---|
claude-sonnet-5-5 | ok | $1.164 / $2.91 | 1M |
claude-opus-5-5 | ok | $2.716 / $9.70 | 1M |
An agent loop sends the growing transcript on every turn, so input tokens dominate its bill; maxTurns is the cheapest guard against a run that wanders.
What the Messages endpoint covers
The gateway documents POST /v1/messages as Anthropic's Messages request passed through to the model: anything Anthropic accepts goes through unchanged, x-api-key and anthropic-version both pass, and stream: true returns Anthropic's own event stream. POST /v1/messages/count_tokens exists too, though its count is a local estimate that errs high, not the model's own figure. That is a pass-through promise, not a claim that every SDK feature has been tested here. Built-in tools such as Read and Bash run on your machine inside the subprocess, so they never depend on the gateway; what reaches the gateway is the model call and its tool definitions.
Check the connection before the agent runs
One direct call with the same key and base proves both before an agent loop hides the error inside a result message:
curl https://api.uplinkswitch.com/v1/messages \
-H "x-api-key: sk-uplink-live-..." \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model": "claude-sonnet-5-5", "max_tokens": 64, "messages": [{"role": "user", "content": "Reply with ok."}]}'A 401 here is the key, a 404 is the model id, and either one will reappear inside the agent until you fix it in this call first. The Claude Code guide covers the interactive CLI with the same two variables, and SDK setup covers the plain Anthropic client when you want to write the tool loop yourself.