Back to Blog
EnglishTutorial

Claude Code Base URL, API Key, and Native Tool Calls: Tested Setup

Configure Claude Code with a native Messages gateway, verify API authentication and a real file-read tool loop, and diagnose base URL and model errors.

C
Crazyrouter Team
July 5, 2026 / 622 views
Share:
Claude Code Base URL, API Key, and Native Tool Calls: Tested Setup

Claude Code needs a gateway that speaks the native Anthropic Messages protocol. Set ANTHROPIC_BASE_URL to the gateway origin, supply a gateway API credential, and verify a real tool round trip before using the setup on a repository.

Crazyrouter is an AI API gateway on which this test verified 2 native Claude operations: a Messages request and a Claude Code file-read task.

This guide was checked on October 11, 2026, using Claude Code 2.1.263, claude-sonnet-4-6, and synthetic files. The examples use https://cn.crazyrouter.com; a separate native request to https://ai.crazyrouter.us also succeeded. For the current product entry point, see the Claude Code integration page.

Which Claude Code base URL should I use?#

Use the origin for Claude Code. It constructs the native API path itself. A different client, such as an OpenAI-compatible SDK, normally expects a base URL ending in /v1.

Client or requestBase URLFinal endpointWhat it verifies
Claude Code through Crazyrouterhttps://cn.crazyrouter.com/v1/messagesNative conversation and client tools
Direct Messages HTTP requestOrigin plus explicit pathhttps://cn.crazyrouter.com/v1/messagesNative request and authentication
OpenAI-compatible clienthttps://cn.crazyrouter.com/v1Depends on API methodIts chosen OpenAI protocol only

Appending /v1 to a client that already adds it can create /v1/v1/messages. Inspect the actual request path when diagnosing a 404. The Messages, Responses, and Chat Completions compared explains why a successful Chat Completions request does not qualify Claude Code.

Claude Code native Messages flow from gateway configuration through a local Read tool and matching result

Configure a gateway API key#

Use a dedicated gateway key with suitable model access. Keep the key in your environment or secret manager; do not put it in a repository. These commands assume CRAZYROUTER_API_KEY already holds that key.

For Bash:

bash
export ANTHROPIC_BASE_URL="https://cn.crazyrouter.com"
export ANTHROPIC_API_KEY="$CRAZYROUTER_API_KEY"
export ANTHROPIC_MODEL="claude-sonnet-4-6"
claude --version

For PowerShell:

powershell
$env:ANTHROPIC_BASE_URL = 'https://cn.crazyrouter.com'
$env:ANTHROPIC_API_KEY = $env:CRAZYROUTER_API_KEY
$env:ANTHROPIC_MODEL = 'claude-sonnet-4-6'
claude --version

The variable assignments above represent the process environment used in the test; the two shell snippets are equivalent setup instructions, not separate operating-system benchmarks. Check that a second credential source is not selecting a different authentication path. The test used --bare, whose help in version 2.1.263 explicitly selects API-key authentication and skips keychain/OAuth reads and several automatic integrations. Normal interactive mode can have additional settings and integrations.

The official Claude Code gateway documentation describes managed authentication and deployment. If your organization already manages these settings, inspect the active configuration before adding overrides. A Claude subscription and a gateway API key are separate billing paths.

Check the live model list with the same credential:

bash
curl -sS https://cn.crazyrouter.com/v1/models \
  -H "Authorization: Bearer $CRAZYROUTER_API_KEY"

The October 11 catalog contained claude-sonnet-4-6; the subsequent request confirmed it was callable. Listing alone is insufficient. Browse the Anthropic model catalog for model context, then verify the exact identifier available to your key.

Verify native Messages before starting a coding task#

This is the request shape used for the successful smoke test. Set the environment variables above, then run:

bash
curl -sS https://cn.crazyrouter.com/v1/messages \
  -H "Authorization: Bearer $CRAZYROUTER_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-sonnet-4-6","max_tokens":60,"messages":[{"role":"user","content":"Reply with exactly gateway-ok."}]}'

The native response returned HTTP 200 and response ID msg_011CfvBkYgRoeEUsa2r8fsSw. Its text answered the synthetic prompt. Authentication, model routing, and this Messages request worked; that result alone says nothing about local file permissions or a long-running coding session.

Verify a real Claude Code tool call#

Create a disposable directory containing only fixture.txt, with this content:

text
Synthetic gateway test. Release code: amber-42.

Run inside that directory:

bash
claude --bare --model claude-sonnet-4-6 \
  --tools Read --allowedTools Read --strict-mcp-config \
  --no-session-persistence --output-format json \
  -p "Read fixture.txt with the Read tool. Return the release code exactly. Do not edit files."

This command restricts the test to a read operation. The run completed in two turns, returned amber-42 in its final answer, reported is_error: false, and recorded no permission denials. It added formatting around the answer despite the instruction to return only the code; this is a successful read task, not an exact-output compliance result.

TestObserved resultScope
Native request through cn.crazyrouter.comHTTP 200Basic Messages route
Native request through crazyrouter.comHTTP 200Alternate origin smoke test
Claude Code 2.1.263 Read taskTwo turns; correct release codeOne local tool loop
Native synthetic ticket tool loopMatching result; final answerExplicit tool_use_id handling

The CLI's reported dollar estimate is not a verified Crazyrouter invoice. It can use list-price assumptions; compare actual account usage when measuring cost. This run did not qualify every model, plugin, MCP server, large repository, or extended session.

Diagnose the layer that failed#

Claude Code troubleshooting matrix covering authentication, duplicate API path, model selection, and tool history

For 401 or 403, check the active credential, its scope, and the host receiving it. For 404, inspect the final path. For a model routing error, check the exact identifier and make a small native request. Retrying a misspelled model cannot repair it.

For missing tool results, preserve the assistant's complete content blocks and return one result for every requested tool ID in the next user message. The tool_use_id and tool_result troubleshooting includes a full validator and runnable round trip. For unexpected token costs, inspect cache counters as described in the prompt-cache miss investigation before attributing the change to output length.

A useful escalation record includes the client version, selected model, endpoint, timestamp, response/request ID, and a redacted error. Never include the API key or private repository contents in a public issue.

FAQ#

Does Claude Code use /v1/chat/completions?#

This native configuration uses /v1/messages. A Chat Completions success can validate a different client but does not establish Claude Code compatibility.

Should ANTHROPIC_BASE_URL include /v1?#

For the tested setup, no: use https://cn.crazyrouter.com. The client adds the native Messages path.

Can I use my Claude subscription as the gateway API key?#

No. Use the credential issued for the gateway's API path. Subscription authentication and gateway API billing are different mechanisms.

Why test a file read after a successful API response?#

A file read exercises the client tool loop and local permissions. Those can fail even when a plain model response succeeds.

Is --bare required for everyday use?#

No. It made this controlled test easier to interpret by skipping automatic integrations. Re-enable your normal features deliberately and verify them under your organization's settings.

Does this prove every Claude model works?#

No. The measured model was claude-sonnet-4-6 on the stated date. Model availability and feature behavior need their own checks.

Implementation Guides

Related Articles

Claude Prompt Cache Not Working? Diagnose Prefixes, TTL, and UsageTutorial

Claude Prompt Cache Not Working? Diagnose Prefixes, TTL, and Usage

Diagnose Claude prompt-cache misses using real cache-write and cache-read token counts. Reproduce a controlled test and separate prefix issues from routing hypotheses.

Oct 11
Claude tool_use_id Errors: Fix Missing and Misordered tool_result BlocksTutorial

Claude tool_use_id Errors: Fix Missing and Misordered tool_result Blocks

Fix Claude tool_use_id and tool_result errors with valid message pairs, a runnable native tool loop, and a local validator for missing, duplicate, and misordered results.

Oct 11
How to Build an AI-Powered Discord Bot with Multiple LLMsTutorial

How to Build an AI-Powered Discord Bot with Multiple LLMs

Complete guide to building a Discord bot that uses GPT-5, Claude, Gemini, and other AI models. Includes code examples, best practices, and cost optimization strategies.

Mar 12
How to Get a Claude API Key in 2026: Secure Setup for Production TeamsTutorial

How to Get a Claude API Key in 2026: Secure Setup for Production Teams

Step-by-step guide to getting a Claude API key, securing it, rotating secrets, and using Crazyrouter as a multi-model alternative.

Jun 5
How to Use JEV 1.13: From Customer-Service Triage to Agent Routing, a Hands-On Test of This Low-Cost Decision ModelTutorial

How to Use JEV 1.13: From Customer-Service Triage to Agent Routing, a Hands-On Test of This Low-Cost Decision Model

JEV 1.13 excels at turning natural language into choices, probabilities, and scores that programs can use directly. Using real-world calls for Chinese customer-service triage, refund decisions, and RAG filtering, this article explains the three usage patterns—Choice, Noul, and Score—and guides you through 12 editable scenarios in the Crazyrouter JEV Decision Playground, where you can inspect results and copy API integration code.

Sep 23
AI Structured Output Guide 2026: JSON Mode Across OpenAI, Claude, and GeminiTutorial

AI Structured Output Guide 2026: JSON Mode Across OpenAI, Claude, and Gemini

Complete developer guide to structured outputs and JSON mode across OpenAI, Claude, and Gemini APIs — with code examples, schema design tips, and a comparison of reliability across providers.

Apr 8