Skip to main content
The Swarms API exposes an OpenAI-compatible 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-key header or Authorization: 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 the messages array:

ContentPart (Multimodal)

When content is an array, each element is a content part: Text part:
Image part:
The 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
  • n must be 1 — 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

When stream: 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

Example Error Response


Code Examples

Non-Streaming Completion

Streaming Completion

Multi-Turn Conversation

Error Handling


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:
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 to AgentCompletion 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

The model 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 usage field in the response shows the exact token counts
Check your balance anytime with GET /v1/account/credits.