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
Note: schema-validation failures on Swarms extension fields passed via
extra_body (e.g. max_loops: 0) currently surface as a 500 server_error rather than a 400 invalid_request_error. Send max_loops >= 1 to avoid this.
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) and streaming_on=False (the agent itself runs to completion; streaming is simulated at the HTTP layer by chunking the result).
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
GET /v1/account/credits.