SDKs
Use Anthropic’s official Python, TypeScript and .NET SDKs to call your deployment’s Agent API. The API uses the
same request and response shapes as Anthropic’s Managed Agents API, so the SDKs work once you point them at your
deployment. Some SDK methods aren’t supported and return 404 or 400. For the full list, see
Coming from Anthropic’s API.
Install the SDK
The examples in these docs use the following SDK versions.
| Language | Package | Version |
|---|---|---|
| Python | anthropic | 1.17.0 |
| TypeScript | @anthropic-ai/sdk | 0.137.0 |
| .NET | Anthropic (NuGet) | 12.44.0 |
The Python and TypeScript examples in the Quickstart and the playbooks
run against a deployment. The cURL examples aren’t run. The other TypeScript examples compile with tsc --strict,
and the other Python examples match the SDK’s signatures. The .NET SDK is tested against every response
and event shape the API sends, with the SDK’s response checks on. For what differs in .NET, see .NET.
Set up the client
Set the base URL to your deployment’s address without a path, for example https://api.context.ai. The SDK adds /v1
itself. Pass your task API key as the API key. To decide which key to create, see
Choose a key.
Set max_retries to 0 in Python, maxRetries to 0 in TypeScript or MaxRetries to 0 in .NET. Sending events
isn’t idempotent, so a retried send can deliver a message twice.
The endpoints are under client.beta: agents,
environments, sessions,
sessions.events, files and
skills. Each endpoint’s reference page shows its SDK method.
Context headers
Some calls accept headers that are specific to Context. Pass them per call with extra_headers in Python or the
headers request option in TypeScript. For example, a service account key can name the session’s owner with the
x-context-owner-user-email header when it creates a session. Without the header, the key’s owner owns the session.
Pagination
When you loop over a list call, the SDK fetches each next page for you. limit sets the page size, within the
Agent API sizes.
Streaming
Stream events says what the stream carries and when to open it. Stop
reading at the first session.status_idle event whose stop_reason.type isn’t requires_action. A requires_action
stop means the agent is waiting for you to approve or deny a tool call. To answer it, see the
tool approvals playbook.
The SDKs don’t reconnect a dropped stream. To resume, reopen it with the Last-Event-ID header set to the ID of the
last event you received. For how a stream ends and reconnects, see
Stream events.
Close each stream when you’re done with it, because each key can have only a few open at once (see Agent API streams).
Errors
An error response raises an exception in Python and throws one in TypeScript. The base class is APIStatusError in
Python and APIError in TypeScript. Each status has its own subclass, such as NotFoundError for 404. The error body, including request_id, is in err.body in Python
and err.error in TypeScript.
.NET
Set BaseUrl to your deployment’s address without a path, ApiKey to your task API key and MaxRetries to 0.
Code
The .NET SDK differs from the other two in a few places:
- Uploads need a filename. Give the
BinaryContentyou upload aFileName. A barebyte[]sends no filename, and the upload returns400. - Context headers go on a per-call client. In 12.44.0 the client’s own
ExtraHeadersisn’t public. Add a header such asx-context-owner-user-emailwithclient.WithOptions(o => o with { ExtraHeaders = ... }), and use that client only to create the session. Every other request refuses the owner header. - The stream skips
session.usage. The SDK’s stream reader drops event names it doesn’t know, andsession.usageis one of them. Read usage from the session (client.Beta.Sessions.Retrieve(id)) or from the event list. - Opening the stream.
client.Beta.Sessions.Events.WithRawResponse.StreamStreaming(id)returns once the stream is open.StreamStreaming(id)sends its request only when you start reading. - The stream’s
errorframe raisesAnthropicSseException, withErrorTypeset to the error’s type.
See also
- Coming from Anthropic’s API lists the calls that return
404or400, the headers that are ignored or refused and the limits that differ. - Stream events shows which frames the stream sends, how it ends and how to reconnect.