本服务面向非中国大陆地区 · ZetaAPI.AI 的网站与控制台主要面向非中国大陆市场,注册与开通功能在你当前地区不可用。

Configure ZetaAPI in Claude Code

Point Claude Code (Anthropic's official CLI) at ZetaAPI in 30 seconds and get 0.3x–0.4x of official pricing, with prompt cache support.

Updated 2026-10-05

When to use this

Claude Code is Anthropic’s official terminal CLI; after installing it with npm i -g @anthropic-ai/claude-code you can call Claude interactively in any local directory. ZetaAPI exposes a /v1/messages endpoint fully compatible with the official Anthropic API, so switching the upstream only takes two required environment variables.

Prerequisites

Setup

1. Set the environment variables

Add to your shell config (~/.zshrc / ~/.bashrc):

export ANTHROPIC_BASE_URL="https://api.staging.cosmos-router.com"
export ANTHROPIC_API_KEY="sk-your-zetaapi-key"
export CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING=1

Keep the third line, CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING=1. Claude Code only streams tool arguments when it talks to Anthropic’s own endpoint; once ANTHROPIC_BASE_URL points elsewhere this is off by default, so writing a large file can go silent for minutes and may end with The response stopped arriving. With it on, tool arguments are streamed as they are generated instead of arriving all at once.

Reload the config:

source ~/.zshrc

2. One-line install (optional)

If you’d rather not edit the shell config by hand, one command does it:

npx --prefer-online zetaapi@latest setup --api-url=https://api.staging.cosmos-router.com claude-code

It writes ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING=1 into the env block of ~/.claude/settings.json (any existing file is backed up as settings.json.bak.<timestamp> first) and verifies connectivity. If env already sets CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING, its existing value is kept. When prompted, paste an Install Token (from the console’s “Setup configuration” / “Connect new tool” dialog, starts with ze_inst_, valid for 5 minutes) or paste an API Key (sk-) directly; for CI / non-interactive runs append --token=ze_inst_xxx or set ZETAAPI_TOKEN.

3. Verify

In any project directory, run:

claude

After your first prompt, the console dashboard immediately shows the call’s token usage and cost.

FAQ

Q: Why am I getting 401 Unauthorized?

Check that ANTHROPIC_API_KEY is a ZetaAPI key (starts with sk-), not an official Anthropic sk-ant- key.

If you configured via npx zetaapi setup --api-url=https://api.staging.cosmos-router.com, the credential lives in env.ANTHROPIC_AUTH_TOKEN inside ~/.claude/settings.json — check that key rather than the environment variable.

Q: Writing a large file goes silent and ends with The response stopped arriving — what should I do?

Claude Code only streams tool arguments when it talks to Anthropic’s own endpoint. Once ANTHROPIC_BASE_URL points elsewhere, tool arguments (such as the contents of a file being written) arrive in one piece only after they are fully generated, so a large file can mean minutes of silence, after which Claude Code gives up on the connection.

Add CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING=1 to your shell profile or to the env block of ~/.claude/settings.json. If you edited your shell profile, reload it (or open a new terminal) before restarting Claude Code. npx zetaapi setup --api-url=https://api.staging.cosmos-router.com claude-code writes it for you.

Q: Is prompt cache supported?

Yes. Claude Code enables ephemeral cache control by default; ZetaAPI passes the original cache behavior through, and cache reads are billed at 10% of the input price — same as official.

Q: Why is Sonnet’s context only 200K? How do I get the full 1M?

When Claude Code points at a third-party base URL, it cannot verify whether the endpoint supports the 1M-token context window, so it conservatively budgets 200K. This is client-side behavior, not a ZetaAPI limit.

Set it explicitly once per session:

/model sonnet[1m]

You can also run /model and pick the entry labeled 1M context. The [1m] marker is a Claude Code directive only — it is stripped before the request is sent, so the model name ZetaAPI receives is still claude-sonnet-5. No extra configuration is needed on our side.

ZetaAPI has served Sonnet contexts of over 600K tokens in production, and Opus up to roughly 900K. Note that very long contexts resend the full history on every turn, so token consumption rises sharply — tune it together with the compact threshold below.

Q: Can I turn off auto-compact?

Yes. Add this to ~/.claude/settings.json:

"autoCompactEnabled": false

Raising the trigger threshold is usually better than turning it off entirely:

/autocompact 500k

Accepted values run from 100K to 1M and are capped at the current model’s context window. For scripts and CI, use the CLAUDE_CODE_AUTO_COMPACT_WINDOW environment variable (plain token count only, e.g. 500000); claude --autocompact 500k applies to a single launch.

Once auto-compact is off, filling the context makes Claude Code raise Prompt is too long and stop, instead of summarizing the history and continuing. If compaction merely feels too frequent, raising the threshold is more practical than disabling it.

Q: How do I switch back to official?

Remove or comment out ANTHROPIC_BASE_URL and set ANTHROPIC_API_KEY back to your official Anthropic key. If you configured via npx zetaapi setup --api-url=https://api.staging.cosmos-router.com, delete ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN from the env block of ~/.claude/settings.json instead (a .bak.<timestamp> backup is available).