Claude Code

Run Anthropic's coding agent on TunAI

Documentation

Use TunAI with Claude Code

Claude Code is Anthropic's coding agent for the terminal. It speaks Anthropic's Messages API, and TunAI speaks OpenAI's, so a small bridge (the open-source LiteLLM proxy) runs on your machine and translates between them. Requests are billed to your TunAI balance in TND.

Anthropic supports Claude Code with Claude models only, so this guide uses TunAI's Claude models.

Before you start

  • An API key. Create one on API keys. A separate key for Claude Code lets you cap its spend.
  • A balance. An agent sends many requests per task. Top up on Billing first.
  • Python 3, to run the bridge.
The Claude model Claude Code will use. Every step below updates.

1Install Claude Code

curl -fsSL https://claude.ai/install.sh | bash

Skip this if claude --version already prints a version.

2Install the bridge

pip install 'litellm[proxy]'

3Tell the bridge about TunAI

Save this as litellm.yaml in any folder:

# litellm.yaml
model_list:
  - model_name: <model-id>
    litellm_params:
      model: openai/<model-id>
      api_base: https://api.tunai.tn/v1
      api_key: os.environ/TUNAI_API_KEY

litellm_settings:
  drop_params: true   # drop request fields TunAI doesn't take
  use_chat_completions_url_for_anthropic_messages: true   # TunAI serves /v1/chat/completions

general_settings:
  master_key: sk-local   # the bridge's own password; LiteLLM won't start without one

model_name is the name Claude Code asks for; keep it the same as the TunAI id so there's one name to remember.

4Start the bridge

In the same folder, export your key and start it. Leave this terminal open.

export TUNAI_API_KEY="YOUR_API_KEY"   # your sk-tunai-... key
litellm --config litellm.yaml --port 4000

Check it from another terminal. A JSON reply means the key, balance and model are fine:

curl -sS http://localhost:4000/v1/messages \
  -H "Authorization: Bearer sk-local" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model": "<model-id>", "max_tokens": 16, "messages": [{"role": "user", "content": "ping"}]}'

5Point Claude Code at the bridge

In a new terminal, in your project:

export ANTHROPIC_BASE_URL="http://localhost:4000"
export ANTHROPIC_AUTH_TOKEN="sk-local"            # the master_key in litellm.yaml
export ANTHROPIC_MODEL="<model-id>"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="<model-id>"   # background tasks, such as session titles
export CLAUDE_CODE_MAX_OUTPUT_TOKENS=16000
claude

Run /status inside Claude Code: the Anthropic base URL line should show http://localhost:4000. Every request appears on the Logs page with its cost.

6Keep the setting

The exports last for one terminal. To keep them, add them to ~/.claude/settings.json:

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://localhost:4000",
    "ANTHROPIC_AUTH_TOKEN": "sk-local",
    "ANTHROPIC_MODEL": "<model-id>",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "<model-id>",
    "CLAUDE_CODE_MAX_OUTPUT_TOKENS": "16000"
  }
}

The bridge still has to be running whenever you use Claude Code this way.

Keep costs under control

  • Set a monthly budget on the key on API keys. Requests stop with a 402 once it's reached.
  • Use a cheaper model for background tasks. Add a second entry to litellm.yaml, for example a Haiku model, and set ANTHROPIC_DEFAULT_HAIKU_MODEL to it.

Troubleshooting

Asks you to log inClaude Code didn't see ANTHROPIC_AUTH_TOKEN. Start claude from the terminal where you exported it, or put it in ~/.claude/settings.json as in step 6.
Connection refusedThe bridge isn't running. Start it as in step 4 and keep that terminal open.
Bridge says “no master key is set” and stopsYour litellm.yaml is missing the general_settings block from step 3. Newer LiteLLM versions refuse to start without a master_key.
404 Not Found for /v1/responses in the bridge logAdd use_chat_completions_url_for_anthropic_messages: true under litellm_settings. Without it, newer LiteLLM versions send Claude Code's requests to an endpoint TunAI doesn't serve.
401 from the bridge, before TunAIANTHROPIC_AUTH_TOKEN must equal the master_key in litellm.yaml.
401 in the bridge logTunAI rejected the key. Check echo $TUNAI_API_KEY in the terminal that runs the bridge, then restart it.
402 insufficient_quotaYour balance is empty or the key reached its monthly budget. Top up on Billing.
404 model_not_foundmodel_name in litellm.yaml and ANTHROPIC_MODEL must both be the exact TunAI id. Copy it from Supported models.
400 on max_tokensKeep CLAUDE_CODE_MAX_OUTPUT_TOKENS at 32000 or below.
400 Extra inputs are not permittedKeep drop_params: true in litellm.yaml, and set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 so Claude Code leaves out pre-release fields.
Back to your Claude accountUnset the variables, or remove them from settings.json. Your claude.ai login stays saved while they're set.