Skip to content

Anthropic SDK

Anthropic’s official SDKs work with OpenPike for Claude models, and for DeepSeek’s Anthropic-compatible endpoint: set the base URL and the key.

Python (anthropic) TypeScript (@anthropic-ai/sdk)
Base URL base_url="https://api.openpike.dev" baseURL: "https://api.openpike.dev"
Key api_key= a virtual key apiKey: a virtual key
Tags on every request default_headers={"x-openpike-tags": ...} defaultHeaders: { "x-openpike-tags": ... }

The base URL has no /v1: the SDK adds /v1/messages itself. The SDK sends the key in x-api-key, which OpenPike accepts.

Terminal window
pip install anthropic # Python
npm install @anthropic-ai/sdk # TypeScript
stream.py
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.openpike.dev",
api_key=os.environ["OPENPIKE_API_KEY"],
default_headers={"x-openpike-tags": "agent=docs-example,env=dev"},
)
with client.messages.stream(
model="anthropic/claude-haiku-5-5",
max_tokens=200,
messages=[{"role": "user", "content": "Count from one to five."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print()

A run budget caps one agent run’s spend, requests and time. Put the run in the client’s default headers, one client per task:

from anthropic import Anthropic
client = Anthropic(
base_url="https://api.openpike.dev",
api_key=virtual_key,
default_headers={"x-openpike-run": task.id, "x-openpike-run-label": task.title},
)
const client = new Anthropic({
baseURL: "https://api.openpike.dev",
apiKey: virtualKey,
defaultHeaders: { "x-openpike-run": task.id, "x-openpike-run-label": task.title },
});

For one request, pass extra_headers={...} in Python or { headers: {...} } as the second argument in TypeScript.

  • Works: messages.create and messages.stream with anthropic/... models, including tools, prompt caching, extended thinking and beta features, and with deepseek/... models for the features DeepSeek’s Anthropic-compatible endpoint supports. anthropic-version and anthropic-beta headers pass through to the provider.
  • Does not work: token counting (messages.count_tokens), the Message Batches API, the Files API and other endpoints besides /v1/messages.
  • Errors come back in Anthropic’s shape. OpenPike’s own 429 blocks use the error type rate_limit_error; a budget block is told apart from Anthropic’s rate limit by its message and by the missing x-openpike-provider header. See Errors. Retries: keep the SDK’s default retries on 429. OpenPike refuses a blocked request before any provider call, so a few retries cost nothing, and they still cover provider 5xx errors. On budget_exceeded, loop_detected, scope_paused or run_limit_exceeded, stop the task and tell the user instead of retrying the same request.

Runnable copies of this setup, with a .env.example, are in the OpenPike examples repo: Python and TypeScript.