Claude Code and Codex CLI on a pay-as-you-go API key
Run Claude Code, Codex CLI and Gemini CLI on one prepaid API key, with prompt caching that stays warm, spend limits per developer and data tools over MCP.
You want Claude Code, Codex CLI or Gemini CLI billed by usage, with a key per developer and no subscription. Change two settings, the base URL and the key, and keep everything else. You pay per token from a prepaid balance, prompt caching keeps working because each session stays on the channel that holds its cache, and the same key gives the agent web search, data tools and a browser over MCP.
How it works
Create a key per developer
In API keys, create one key for each person or CI job and give it a spend limit. Use in tools on any key opens ready-made setups for each CLI, filled in with the key.
Point your agent at the gateway
Set the base URL and the key in your shell or the agent's config file. Each CLI talks the API it was built for: Anthropic, OpenAI or Gemini.
Code as usual
Every session is pinned to one channel so its prompt cache stays warm. Logs show each call's tokens, cache hits and charge, per key.
Set up your coding agent
Claude Code#
Export the settings and start it, or put them in ~/.claude/settings.json to keep them across sessions:
export ANTHROPIC_BASE_URL=https://lovelyrouter.com
export ANTHROPIC_AUTH_TOKEN=$LOVELY_API_KEY
export ANTHROPIC_MODEL=claude-sonnet-5.5
claudeANTHROPIC_MODEL is the main model and ANTHROPIC_DEFAULT_HAIKU_MODEL handles quick background tasks; any Claude model id works, such as claude-opus-5.5. See the Claude Code guide and the Claude API page.
Codex CLI#
Codex CLI uses the OpenAI Responses API. Add Lovely Router as a model provider, then export the key named in env_key:
model = "gpt-6.1-sol"
model_provider = "lovely"
[model_providers.lovely]
name = "Lovely Router"
base_url = "https://lovelyrouter.com/v1"
env_key = "LOVELY_API_KEY"
wire_api = "responses"export LOVELY_API_KEY="lr-..."
codexUse a GPT model (gpt-*): those are the models served over the Responses API. Current Codex versions no longer accept wire_api = "chat". See the Codex CLI guide and GPT models.
Gemini CLI#
export GOOGLE_GEMINI_BASE_URL=https://lovelyrouter.com
export GEMINI_API_KEY=$LOVELY_API_KEY
gemini -m gemini-3.1-proWhy conversation pinning decides your bill
A coding agent resends the whole conversation on every turn: the system prompt, the tool definitions and every file it has read so far. With prompt caching, most of those input tokens are billed at the cache-read price, a fraction of the normal input price. But a cache lives on the channel that wrote it. If a turn lands on another channel, the cache isn't there, and the whole prefix is paid for again.
The gateway keeps each conversation on the channel that served its first turn:
sends an x-claude-code-session-id header and Codex a prompt_cache_key for each session; the gateway uses them to recognize the turns of one conversation.
and is renewed while the session is active. After 30 to 60 minutes without a request it expires.
The turn waits for its pinned channel briefly (about a second for short prompts, up to ten seconds for long ones) rather than moving and paying for a new cache write.
Each call in Logs shows its cached tokens, cache hit rate and affinity (sticky, new or explicit). Prompt caching has the details.
A key per developer, with its own limits
All keys draw on one balance, but each has its own limits, and the key list shows what each one has spent.
| Limit | What it does |
|---|---|
| Spend limit | The most the key may ever spend, across models, data tools and the browser agent. A lifetime cap, not a monthly budget. |
| Requests per minute, max concurrent requests | Slow a runaway loop or keep one person from starving the others. |
| Model allow-list | Only the models this key may call. |
| Expires on | The key stops working after that day. |
A key that reaches its spend limit gets 402 with This API key has reached its spend limit.; a rate or concurrency limit returns 429 with a Retry-After header. Refused requests are not billed. See API keys.
Give your coding agent the web, data tools and a browser
The same key connects the MCP server, so the agent can search the web, read a docs page as Markdown, pull structured data from Google Maps, LinkedIn or Reddit, and drive a browser through a multi-step flow:
claude mcp add --transport http lovely-tools https://lovelyrouter.com/mcp \
--header "Authorization: Bearer $LOVELY_API_KEY"Search the web: up to 50 results with title, URL, date, site, a short snippet and a long summary written for models (and the page text if asked). Operators: site:, -site:, "exact phrase", -word, lang:, after:, before:.
Read any public web page as clean Markdown: its title and text, without navigation or ads. For one page you already have the URL of.
Crawl a whole website (or a section of it) and return each page as clean, LLM-ready markdown. Good for RAG and documentation sites.
Give it a goal and a start page; an AI agent drives a real browser to do it: navigate, fill forms, log in with saved secrets, ask you for codes, hand over for CAPTCHAs, wait for your approval before purchases, and return structured JSON in your schema. For tasks that need interaction; to just read a page, use Read a web page (web-fetch). $0.006 per step in fast mode, $0.012 in accurate mode (looks at screenshots when unsure, can run read-only code to read data). Each step includes 1 minute; time beyond that (e.g. waiting for your approval) costs $0.005/min. Full API: /v1/browser.
Web search covers lookups, web fetch reads one page and website crawl a whole docs section. The browser agent works in the cloud, so it handles public sites, not localhost.
Setups that work well
Set ANTHROPIC_MODEL=claude-opus-5.5 and keep ANTHROPIC_DEFAULT_HAIKU_MODEL on a small model for background tasks.
An expiry date, a spend limit and an allow-list with one model.
Its own key with a spend limit and a requests-per-minute limit, so a loop can't run up a bill.
Ask: "Read the changelog at this URL and update our SDK calls to match." One web_fetch gives the agent the page.
What you pay
Model calls are billed per token: input, output, cache reads and cache writes, each at the model's price per million tokens. Requests that return an error are not charged. Data tools are billed per result and the browser agent per step, from the same balance. There is no subscription and no automatic recharge: you top up from $5 and are never billed more than you added. See pricing and the full model list.
| Model | Context | Input | Output | Cache read |
|---|---|---|---|---|
| Claude Fable 5.1 claude-fable-5.1 | 1M | $10.00 | $50.00 | $0.25 |
| Claude Opus 5.5 claude-opus-5.5 | 1M | $4.00 | $20.00 | $0.20 |
| Claude Sonnet 5.5 claude-sonnet-5.5 | 1M | $2.00 | $10.00 | $0.10 |
| Claude Haiku 5.5 claude-haiku-5.5 | 1M | $0.10 | $0.50 | $0.01 |
| Claude Fable 5 claude-fable-5 | 1M | $10.00 | $50.00 | $1.00 |
| Claude Opus 5 claude-opus-5 | 1M | $5.00 | $25.00 | $0.50 |
| Claude Sonnet 5 claude-sonnet-5 | 1M | $2.00 | $10.00 | $0.20 |
| Claude Opus 4.8 claude-opus-4.8 | 1M | $5.00 | $25.00 | $0.50 |
| Claude Opus 4.7 claude-opus-4.7 | 1M | $5.00 | $25.00 | $0.50 |
| Claude Opus 4.6 claude-opus-4.6 | 1M | $5.00 | $25.00 | $0.50 |
| Claude Sonnet 4.6 claude-sonnet-4.6 | 1M | $3.00 | $15.00 | $0.30 |
| Claude Opus 4.5 claude-opus-4.5 | 200K | $5.00 | $25.00 | $0.50 |
| Claude Sonnet 4.5 claude-sonnet-4.5 | 1M | $3.00 | $15.00 | $0.30 |
| Claude Haiku 4.5 claude-haiku-4.5 | 200K | $1.00 | $5.00 | $0.10 |
USD per 1M tokens, live from the catalog.
| Model | Context | Input | Output | Cache read |
|---|---|---|---|---|
| GPT-6 Astra gpt-6-astra | 1.05M | $10.00 | $50.00 | $1.00 |
| GPT-6.1 Sol gpt-6.1-sol | 1.05M | $2.00 | $10.00 | $0.10 |
| GPT-6 Sol gpt-6-sol | 1.05M | $2.00 | $10.00 | $0.20 |
| GPT-6.1 Terra gpt-6.1-terra | 1.05M | $1.00 | $5.00 | $0.05 |
| GPT-5.6 Sol gpt-5.6-sol | 400K | $2.00 | $10.00 | $0.20 |
| GPT-5.6 Terra gpt-5.6-terra | 400K | $2.00 | $12.00 | $0.20 |
| GPT-5.6 Luna gpt-5.6-luna | 400K | $0.20 | $1.20 | $0.02 |
USD per 1M tokens, live from the catalog.
Frequently asked questions
Can I use Claude Code with a pay-as-you-go API key?+
Yes. Set ANTHROPIC_BASE_URL to https://lovelyrouter.com and ANTHROPIC_AUTH_TOKEN to your Lovely Router key, and Claude Code is billed per token from your prepaid balance, with no subscription.
Does prompt caching work with Claude Code and Codex?+
Yes. Request fields such as cache_control and prompt_cache_key pass through unchanged, and the gateway keeps every turn of a session on the channel that holds its cache, using the session id Claude Code sends or the prompt_cache_key Codex sends.
Which models can Codex CLI use?+
GPT models (gpt-*), because Codex uses the OpenAI Responses API and that API serves GPT models. Set wire_api = "responses" in the provider config.
How do I limit what each developer spends?+
Give each developer their own key with a spend limit, and optionally a requests-per-minute limit, a model allow-list and an expiry date. That is how Claude Code for teams gets a cap per person. When the key reaches its limit, requests fail with 402 and nothing more is charged.
Can I use Gemini CLI too?+
Yes. Set GOOGLE_GEMINI_BASE_URL to https://lovelyrouter.com and GEMINI_API_KEY to your key, then start it with a Gemini model such as gemini-3.1-pro. See the Gemini CLI guide and the Gemini API page.
Running many agents at once? See high-concurrency LLM apps. Every other client and SDK is covered in Use with your tools.