Skip to main content
The arbytra Python package provides an OpenAI-compatible client for the Arbytra API.

Full SDK Reference

Complete API reference with all types, parameters, and examples

Installation

Requires Python 3.10 or later.

Get started

Configure

API Key

Base URL

Timeout

Retries

Create chat completions

Basic request

Send a chat completion request:

With routing options

You can also pass a RoutingOptions object for IDE autocomplete and validation:
All routing fields: See Advanced Routing for detailed strategy guides.

Multi-model routing

Route a request across multiple models. The router picks the best option based on your routing strategy:
model and gateway.models are mutually exclusive. Specify exactly one. Passing both raises BadRequestError.

Reasoning effort

Enable extended reasoning for complex tasks using the reasoning_effort parameter:
You can also pass provider-specific parameters through extensions:
See Extensions and Thinking for provider details and streaming thinking output.

Request metadata

Attach metadata to requests for tracking and analytics:
Valid metadata fields: user_id, tags (list), trace_id, and custom_fields (dict for arbitrary key-value pairs). See the Python SDK Reference for field constraints.

Stream responses

After consuming all chunks, access stream-level metadata:
Use a context manager for automatic cleanup:
Or close manually with stream.close().
Routing metadata, usage, and response headers are available only after consuming all chunks.
See Streaming Guide for full patterns including tool call streaming.

Tool calling

See Tool Calling Guide for multi-turn tool conversations.

Read response headers

Every response and error includes a response_headers object with typed accessors:
Error objects also carry response_headers. Use e.response_headers.request_id when filing support tickets to correlate with server logs. See the Python SDK Reference for the complete ResponseHeaders API.

Read token usage

The Usage object on every response carries optional detail breakdowns:
Availability depends on the provider. completion_tokens_details.reasoning_tokens is present for OpenAI o-series, DeepSeek, xAI, and Google Gemini. It’s None for providers that don’t report reasoning token counts (Anthropic, Moonshot, Fireworks). See Check reasoning token availability for the full breakdown.

Handle errors

Catch typed exceptions:
See Error Handling Guide for retry patterns and map_openai_error().

Use identity and model discovery APIs

Query identity and model information:

Model listing choices

See the Python SDK Reference for the complete API.

Use async client

Use the async client for non-blocking requests:

Async streaming

Stream responses asynchronously:

Async context manager

Use async with for automatic connection cleanup:
Or close explicitly: await client.close()

Use with OpenAI-compatible frameworks

ArbytraAsyncOpenAI (experimental) is an AsyncOpenAI subclass that captures routing metadata automatically. Pass it to any framework that accepts an external AsyncOpenAI instance. The kwarg name varies across frameworks. Install with the optional openai-compat extra:

Basic usage

Call it directly like any AsyncOpenAI client, then read last_routing_metadata on the client after the response completes:

Capture metadata per request

last_routing_metadata is a single-slot property. Under concurrent use it reflects the most recent response. For per-request capture, pass an on_response callback:
The callback must be synchronous. An async callable raises TypeError at construction.

Pass routing options

Pass routing options via the extra_body kwarg. RoutingOptions.to_extra_body() returns a dict shaped for the Arbytra API:
RoutingOptions lives in arbytra.route_types. It is not exported at top-level.

Framework wiring

Each supported framework accepts an external AsyncOpenAI instance via its own kwarg: LangChain takes the chat.completions resource rather than the full client. LangChain and LlamaIndex both still require an api_key argument for their own parent-class construction; pass any placeholder value. For the Agents SDK path, see OpenAI Agents SDK. For the full class reference, see ArbytraAsyncOpenAI.

ArbytraAsyncOpenAI (experimental) or AsyncClient?

Use ArbytraAsyncOpenAI when a framework needs an AsyncOpenAI instance. Use arbytra.AsyncClient for direct Python code. AsyncClient exposes routing_metadata directly on each response, so you do not need to read a separate client-level property.
ArbytraAsyncOpenAI is Python-only. TypeScript consumers can use @arbytra/ai-sdk-provider with the Vercel AI SDK, or the OpenAI TS SDK with baseURL: 'https://api.arbytra.com/v1'.

Use context managers

Use a context manager for automatic cleanup:

SDK scope

The Arbytra SDK covers: inference (chat completions and the Response API, both with routing), identity, and model discovery. For full platform operations, use the REST API directly.

Use type hints

The SDK provides typed responses, errors, and routing configuration. Use your IDE’s autocomplete for the best experience: