POST /v1/chat/completions endpoint. If your application already uses the OpenAI SDK, you can switch to Swarms by changing two lines — the base_url and api_key — and everything else works unchanged.
Under the hood, every request is routed through the full Swarms agent infrastructure: model routing, token counting, billing, and logging all apply exactly as they do for the native /v1/agent/completions endpoint.
Endpoint Information
- URL:
/v1/chat/completions - Method:
POST - Authentication: Required (
x-api-keyheader orAuthorization: Bearer <key>) - Rate Limiting: Subject to tier-based rate limits
Authentication
Two authentication methods are supported. Both work on all Swarms API endpoints.
The Bearer token method is what the OpenAI SDK sends by default, so it works out of the box.
Get your API key at swarms.world/platform/api-keys.
Request Schema
ChatCompletionRequest Object
ChatMessage Object
Each message in themessages array:
ContentPart (Multimodal)
Whencontent is an array, each element is a content part:
Text part:
url field accepts both HTTPS URLs and base64-encoded data URIs (data:image/png;base64,...).
Validation Rules
- At least one message with
role: "user"is required nmust be1— multiple completions per request are not supported (send separate requests instead)- Requests with zero messages or only system messages are rejected
Example Request Body
Response Schema
ChatCompletionResponse Object (Non-Streaming)
Choice Object
CompletionUsage Object
Example Response
Streaming Response Schema
Whenstream: true is set, the response is returned as Server-Sent Events (SSE). Each event is a data: line containing a JSON chunk.
StreamChunk Object
StreamChoice Object
Stream Sequence
Example Stream
Error Response Schema
Errors are returned in the standard OpenAI error format so the OpenAI SDK’s built-in error classes work correctly:Error Object
Error Types
All request-body validation failures on this endpoint — including out-of-range Swarms extension fields passed via
extra_body (e.g. max_loops: 0, which violates 1 <= max_loops <= 50) — are caught by a dedicated handler and returned as 400 invalid_request_error in the OpenAI error shape above, not the raw FastAPI/Pydantic {"detail": [...]} shape used elsewhere in the Swarms API.
Example Error Response
Code Examples
Non-Streaming Completion
- Python
- TypeScript
- Go
- Rust
- cURL
Streaming Completion
- Python
- TypeScript
- Go
- Rust
- cURL
Multi-Turn Conversation
- Python
- TypeScript
- Go
- Rust
Error Handling
- Python
- TypeScript
- Go
- Rust
Multi-Loop Reasoning
By default the agent runs a single pass (max_loops=1). To let the agent iterate on its own output — useful for complex reasoning, self-correction, or multi-step tasks — pass max_loops via the OpenAI SDK’s extra_body parameter:
- Python
- TypeScript
- cURL
max_loops is a Swarms extension field — it is not part of the OpenAI API spec. In the Python OpenAI SDK, use extra_body={"max_loops": N} to pass it. In cURL or raw HTTP, include it directly in the JSON body.How It Maps to Swarms Internals
For users already familiar with the native Swarms API, here is how the OpenAI request fields map toAgentCompletion and AgentSpec:
The agent is created with
max_loops set to the requested value (defaults to 1 for single-turn). For a non-streaming request the agent runs with streaming_on=False and the full response is returned in one JSON body. For stream: true, the agent config is rebuilt with streaming_on=True and the agent’s own token-by-token output is forwarded live, via a streaming callback, as SSE chunks — the same mechanism /v1/agent/completions uses when streaming_on is set directly.
Supported Models
Themodel field accepts any model supported by the Swarms API. Common options:
For the full list, call
GET /v1/models (standard OpenAI list format — works with client.models.list() in the OpenAI SDK) or GET /v1/models/available (Swarms format with a count field) with your API key. Both return the same catalog, filtered by your subscription tier:
Differences from the OpenAI API
Billing
Usage is metered and billed identically to the native/v1/agent/completions endpoint:
- Input tokens are counted from the combined system prompt, conversation history, and task
- Output tokens are counted from the agent’s response
- Credits are deducted automatically after each completion
- The
usagefield in the response shows the exact token counts /v1/chat/completionsis billable: it requires a total credit balance greater than $1.00, or the request is rejected before the agent runs
GET /v1/account/credits.