# OSMEXO integration guide

> For AI coding assistants (Claude, ChatGPT, Codex, Cursor and others). Task: switch this project's LLM calls to OSMEXO.

## What OSMEXO is

OSMEXO is an OpenAI-compatible API for many AI models: GPT, Claude, Gemini, DeepSeek, Qwen, Kimi, GLM and more. One API key works for all of them, and each request is billed below the model vendor's list price (prepaid credits, pay as you go). Code that uses the OpenAI SDK or any OpenAI-compatible client only needs a configuration change.

## The three things that change

| Setting | Before | After |
|---|---|---|
| Base URL | `https://api.openai.com/v1` | `https://api.osmexo.com/v1` |
| API key | `sk-…  (vendor key)` | `sk-llm-…  (OSMEXO key)` |
| Model id | `gpt-5.4-mini` | `openai/gpt-5.4-mini` |

Everything else stays the same: request and response bodies, streaming (SSE ending with `data: [DONE]`), tool calling (`tools`, `tool_choice`, `tool_calls`), `response_format` (JSON mode and JSON schema), `temperature`, `max_tokens`, `stop`, `seed`.

## Steps for the assistant

1. Find every place the project creates an OpenAI (or OpenAI-compatible) client or calls a chat/completions API.
2. Point the client at `https://api.osmexo.com/v1` and read the key from the environment variable `OSMEXO_API_KEY`. Never hard-code the key.
3. Add the vendor prefix to every model id (`gpt-5.4-mini` → `openai/gpt-5.4-mini`). Keep model ids in configuration.
4. Leave prompts, message formats, tool definitions and response parsing unchanged.
5. Treat HTTP 402 (out of credit) as "top up your OSMEXO balance": log or show it clearly and do not retry.
6. Retry 429 and 5xx with backoff, honouring the `Retry-After` header.
7. Add `OSMEXO_API_KEY=` to `.env.example` and mention it in the README.

## Python

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.osmexo.com/v1",
    api_key=os.environ["OSMEXO_API_KEY"],
)

# Any model: "anthropic/claude-sonnet-5", "deepseek/deepseek-v4.1-flash-1017", ...
resp = client.chat.completions.create(
    model="openai/gpt-5.4-mini",
    messages=[{"role": "user", "content": "Summarise this: ..."}],
)
print(resp.choices[0].message.content)
```

## TypeScript / Node.js

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.osmexo.com/v1",
  apiKey: process.env.OSMEXO_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "openai/gpt-5.4-mini",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(resp.choices[0].message.content);
```

## Streaming (Python)

```python
stream = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash-1017",
    messages=[{"role": "user", "content": "Write a haiku about budgets."}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
```

## curl

```bash
curl https://api.osmexo.com/v1/chat/completions \
  -H "Authorization: Bearer $OSMEXO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"openai/gpt-5.4-mini","messages":[{"role":"user","content":"Hello"}]}'
```

## Without changing code

```bash
# Most OpenAI SDKs read these, so no code change is needed
# (only add the vendor/ prefix to your model ids)
export OPENAI_BASE_URL="https://api.osmexo.com/v1"
export OPENAI_API_KEY="sk-llm-..."
```

## Frameworks

LangChain, LlamaIndex, Vercel AI SDK, LiteLLM and similar: use their OpenAI or "OpenAI-compatible" provider with the base URL and key above. LangChain example:

```python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(base_url="https://api.osmexo.com/v1", api_key=os.environ["OSMEXO_API_KEY"], model="openai/gpt-5.4-mini")
```

## Coding agents

```text
# Cline, Roo Code, OpenCode, Aider, Continue:
# choose the "OpenAI compatible" provider and set
Base URL:  https://api.osmexo.com/v1
API key:   your OSMEXO key (sk-llm-...)
Model:     deepseek/deepseek-v4.1-flash-1017   # or anthropic/claude-sonnet-5
```

## Model ids

Ids are `vendor/model`. Examples:

| Use | Model ids |
|---|---|
| Fast, cheap everyday tasks: summaries, extraction, classification | `openai/gpt-5.4-mini`, `google/gemini-3-5-flash-lite`, `deepseek/deepseek-v4.1-flash-1017` |
| Strong general reasoning | `openai/gpt-5.5`, `anthropic/claude-sonnet-5`, `google/gemini-3.1-pro` |
| The hardest problems | `anthropic/claude-opus-5.5` |
| Coding and agents | `deepseek/deepseek-v4.1-flash-1017`, `z-ai/glm-5.3`, `qwen/qwen3-coder-next` |

All models and prices: https://osmexo.com/models. List for your key: `curl https://api.osmexo.com/v1/models -H "Authorization: Bearer $OSMEXO_API_KEY"`

## Errors

Errors use the OpenAI error shape, so OpenAI SDK error classes work as usual:

```json
{ "error": { "type": "quota_error", "code": "insufficient_credits", "message": "...", "request_id": "req_..." } }
```

| HTTP | Meaning | What to do |
|---|---|---|
| 400 | Invalid request (bad parameter or JSON) | Fix the request |
| 401 | Missing, wrong or revoked API key | Check OSMEXO_API_KEY |
| 402 | Out of credit, or a key budget or spending limit reached | Top up in the dashboard; do not retry |
| 403 | The key may not use this model or call from this IP | Check the key settings |
| 404 | Unknown model id | Check the id with GET /v1/models |
| 429 | Rate limited | Wait for Retry-After, then retry |
| 5xx | Temporary problem on our side or the model’s | Retry with backoff |

Failed requests are never billed. Quote `request_id` (also in the `X-Request-Id` header) when contacting support.

## Account and billing

- Sign up and create a key: https://dashboard.osmexo.com/signup (new accounts get free credit).
- Prepaid credits from $5 by PayPal or Visa/Mastercard; no subscription; credits never expire.
- The dashboard shows every request with its cost and the saving against the vendor's list price.
- Documentation: https://docs.osmexo.com
- This guide as a web page: https://osmexo.com/how-it-works
