Use case

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.

Prepaid, no subscriptionREST or MCPOne key for models, data and agents
claude code$ export ANTHROPIC_BASE_URL=https://lovelyrouter.com$ export ANTHROPIC_AUTH_TOKEN=$LOVELY_API_KEY$ claude> Fix the flaky test in billing.test.ts ⏺ Read billing.test.ts · Grep "settle" ⏺ Edit src/billing.ts (+6 −2) cache read 41,280 tokens · write 1,212 ✓ npm test — 214 passed

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.

01

How it works

010203
  1. 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.

  2. 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.

  3. 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.

02

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
claude

ANTHROPIC_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:

~/.codex/config.toml
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"
Shell
export LOVELY_API_KEY="lr-..."
codex

Use 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#

Shell
export GOOGLE_GEMINI_BASE_URL=https://lovelyrouter.com
export GEMINI_API_KEY=$LOVELY_API_KEY
gemini -m gemini-3.1-pro
03

Why 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:

Claude Code

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.

A pin lasts up to an hour

and is renewed while the session is active. After 30 to 60 minutes without a request it expires.

A busy channel doesn't break the cache

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.

04

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.

LimitWhat it does
Spend limitThe 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 requestsSlow a runaway loop or keep one person from starving the others.
Model allow-listOnly the models this key may call.
Expires onThe 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.

05

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"

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.

06

Setups that work well

Opus for the hard work, Haiku for the rest

Set ANTHROPIC_MODEL=claude-opus-5.5 and keep ANTHROPIC_DEFAULT_HAIKU_MODEL on a small model for background tasks.

A contractor key

An expiry date, a spend limit and an allow-list with one model.

An agent in CI

Its own key with a spend limit and a requests-per-minute limit, so a loop can't run up a bill.

Read before you code

Ask: "Read the changelog at this URL and update our SDK calls to match." One web_fetch gives the agent the page.

07

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.

ModelContextInputOutputCache 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.

ModelContextInputOutputCache 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.

08

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.

One prepaid key for models, data and agents

No subscription. Top up, use what you need, see every charge in your logs.