> ## Documentation Index
> Fetch the complete documentation index at: https://docs.a21e.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Compatibility

> Drop-in replacement for the OpenAI chat completions API.

## Overview

a21e exposes an OpenAI-compatible `/v1/chat/completions` endpoint. If your application already uses the OpenAI SDK, you can switch to a21e by changing two lines — the base URL and API key.

## Setup

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_A21E_API_KEY",
      base_url="https://api.a21e.com/v1",
  )

  response = client.chat.completions.create(
      model="auto",
      messages=[
          {"role": "user", "content": "Explain dependency injection in Go"}
      ],
  )
  ```

  ```typescript TypeScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: "YOUR_A21E_API_KEY",
    baseURL: "https://api.a21e.com/v1",
  });

  const response = await client.chat.completions.create({
    model: "auto",
    messages: [
      { role: "user", content: "Explain dependency injection in Go" },
    ],
  });
  ```

  ```bash cURL theme={null}
  curl https://api.a21e.com/v1/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "auto",
      "messages": [
        {"role": "user", "content": "Explain dependency injection in Go"}
      ]
    }'
  ```
</CodeGroup>

## Model mapping

The `model` parameter maps to a21e's tier system:

| Value                           | a21e tier | Behavior                                   |
| ------------------------------- | --------- | ------------------------------------------ |
| `auto`                          | Auto      | a21e selects the best model                |
| `economy`                       | Economy   | Fastest, lowest cost                       |
| `standard`                      | Standard  | Balanced                                   |
| `premium`                       | Premium   | Highest quality                            |
| Any model name (e.g., `gpt-4o`) | Auto      | Accepted but a21e selects the actual model |

## What's different from raw OpenAI

When you use a21e's OpenAI-compatible endpoint, your request gets the full a21e pipeline:

* **Prompt synthesis** — your message is enhanced with curated techniques
* **Memory injection** — relevant memories from past sessions are applied
* **Workspace context** — preferences, persona, and repo context are included
* **Quality scoring** — outputs are scored for continuous improvement

The response format matches the OpenAI spec exactly, so existing integrations work without changes.

## Streaming

Streaming is supported via the standard `stream: true` parameter:

```python theme={null}
stream = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Write a Makefile for a Go project"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
```
