Skip to main content

Claude Code Native

The claude_code_native provider is a specialized variant of Anthropic designed for Claude Code, Anthropic's official CLI for Claude. This provider uses Bearer authentication and automatically includes a specific system prompt optimized for CLI workflows.

Key Differences from Anthropic​

FeatureAnthropicsClaude Code Native
Authenticationx-api-key headerAuthorization: Bearer <token> header
Required Headersanthropic-versionanthropic-version, anthropic-beta: oauth-2025-04-20
System PromptUser-definedAlways prepends: "You are Claude Code, Anthropic's official CLI for Claude."
API Endpointhttps://api.anthropic.com/https://api.anthropic.com/ (same endpoint, different auth/headers)
PropertyDetails
DescriptionSpecialized Anthropic provider for Claude Code CLI with OAuth authentication and system prompt
Provider Route on LiteLLMclaude_code_native/ (add this prefix to model names)
Provider DocClaude Code ↗
API Endpointhttps://api.anthropic.com
Supported Endpoints/chat/completions, /v1/messages (passthrough)

Supported Models​

Not all Anthropic models are supported through the claude_code_native provider. The supported models are

  • claude-sonnet-4-5
  • claude-opus-4-5
  • claude-haiku-4-5

Supported OpenAI Parameters​

The claude_code_native provider supports all Anthropic parameters:

"stream",
"stop",
"temperature",
"top_p",
"max_tokens",
"max_completion_tokens",
"tools",
"tool_choice",
"extra_headers",
"parallel_tool_calls",
"response_format",
"user",
"reasoning_effort",
info

Notes:

  • The provider automatically adds the required system prompt for Claude Code
  • All system messages provided by the user are appended after the Claude Code system prompt
  • The provider requires an OAuth bearer token (not an API key)
  • max_tokens defaults to 4096 if not specified

API Keys and Authentication​

The claude_code_native provider uses OAuth Bearer authentication and requires the CLAUDE_CODE_API_KEY environment variable:

import os

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token" # Used as Bearer token
# os.environ["CLAUDE_CODE_API_BASE"] = "" # [OPTIONAL] Custom API base
note

The provider automatically converts the API key to a Bearer token by setting the Authorization: Bearer <token> header instead of x-api-key: <token>.

info

Priority for API Key:

  1. Explicit api_key parameter in completion calls takes highest priority
  2. The CLAUDE_CODE_API_KEY environment variable is required if no explicit api_key is provided

Getting a Long-Lived Token​

The claude_code_native provider is designed to work seamlessly with Claude Code, Anthropic's official CLI. Using Claude Code eliminates the need for separate API keys - the long-lived token approach is both cache and token efficient, with improved implementation that speaks directly to the Claude API.

Preparing to Configure​

  1. Install Claude Code using your preferred method:
  • macOS or Linux (curl): curl -fsSL https://claude.ai/install.sh | bash
  • macOS (Homebrew): brew install --cask claude-code
  • Windows (PowerShell): irm https://claude.ai/install.ps1 | iex
  1. Login and get a long-lived token (valid for one year):
claude setup-token
  1. Record the token value - it will not be shown again.

  2. Set the environment variable:

export CLAUDE_CODE_API_KEY="your-long-lived-token-from-claude-setup-token"

It can also be entered in the UI in the normal fashion.

tip

The long-lived token obtained from claude setup-token is the recommended way to authenticate with the claude_code_native provider. It's valid for one year and provides a secure, cache-efficient way to interact with Claude models.

Supported Models​

The claude_code_native provider supports the following Claude models:

  • Claude Sonnet 4.5 (claude-sonnet-4-5) - Latest, recommended for most use cases
  • Claude Opus 4.5 (claude-opus-4-5) - Most capable model
  • Claude 4.5 Haiku (claude-haiku-4-5) - Fast responses for quick tasks
info

The implementation uses an updated technique that speaks directly to the Claude API, eliminating many downsides of previous implementations with better cache and token efficiency.

Usage​

Basic Usage​

import os
from litellm import completion

# Set your OAuth bearer token
os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

messages = [{"role": "user", "content": "Hey! how's it going?"}]
response = completion(model="claude_code_native/claude-opus-4-20250514", messages=messages)
print(response)

Usage - Streaming​

import os
from litellm import completion

# Set your OAuth bearer token
os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

messages = [{"role": "user", "content": "Hey! how's it going?"}]
response = completion(model="claude_code_native/claude-opus-4-20250514", messages=messages, stream=True)
for chunk in response:
print(chunk["choices"][0]["delta"]["content"]) # same as openai format

Usage with Custom System Message​

When you provide your own system message, it will be appended after the Claude Code system prompt:

import os
from litellm import completion

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Help me debug this code."}
]
response = completion(model="claude_code_native/claude-3-5-sonnet-20240620", messages=messages)
print(response)

The actual system messages sent to Anthropic will be:

  1. "You are Claude Code, Anthropic's official CLI for Claude." (auto-added)
  2. "You are a helpful coding assistant." (user-provided)

Usage with LiteLLM Proxy​

1. Save key in your environment​

export CLAUDE_CODE_API_KEY="your-oauth-bearer-token"

2. Start the proxy​

model_list:
- model_name: claude-code-sonnet-4 ### RECEIVED MODEL NAME ###
litellm_params: # all params accepted by litellm.completion()
model: claude_code_native/claude-sonnet-4-20250514 ### MODEL NAME sent to `litellm.completion()` ###
api_key: "os.environ/CLAUDE_CODE_API_KEY" # does os.getenv("CLAUDE_CODE_API_KEY")
litellm --config /path/to/config.yaml

3. Test it​

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "claude-code-sonnet-4",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}
'

Supported Models​

Model NameFunction CallOAuth Required
claude-sonnet-4-5completion('claude_code_native/claude-sonnet-4-5-20250929', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-opus-4completion('claude_code_native/claude-opus-4-20250514', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-sonnet-4completion('claude_code_native/claude-sonnet-4-20250514', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-3.7completion('claude_code_native/claude-3-7-sonnet-20250219', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-3-5-sonnetcompletion('claude_code_native/claude-3-5-sonnet-20240620', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-3-haikucompletion('claude_code_native/claude-3-haiku-20240307', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-3-opuscompletion('claude_code_native/claude-3-opus-20240229', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-3-sonnetcompletion('claude_code_native/claude-3-sonnet-20240229', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-2.1completion('claude_code_native/claude-2.1', messages)os.environ['CLAUDE_CODE_API_KEY']
claude-2completion('claude_code_native/claude-2', messages)os.environ['CLAUDE_CODE_API_KEY']

Function/Tool Calling​

The claude_code_native provider supports all Anthropic tool calling features:

from litellm import completion
import os

# Set OAuth bearer token
os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]

response = completion(
model="claude_code_native/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="auto",
)
print(response)

Prompt Caching​

The claude_code_native provider supports Anthropic's prompt caching feature. See the Anthropic documentation for detailed examples with the cache_control parameter.

response = await litellm.acompletion(
model="claude_code_native/claude-3-5-sonnet-20240620",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an AI assistant tasked with analyzing legal documents.",
},
{
"type": "text",
"text": "Here is the full text of a complex legal agreement",
"cache_control": {"type": "ephemeral"},
},
],
},
{
"role": "user",
"content": "what are the key terms and conditions in this agreement?",
},
]
)

Structured Outputs​

The claude_code_native provider supports Anthropic's structured outputs feature. See the Anthropic documentation for details.

from litellm import completion

response = completion(
model="claude_code_native/claude-sonnet-4-5-20250929",
messages=[{"role": "user", "content": "What is the capital of France?"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "capital_response",
"strict": True,
"schema": {
"type": "object",
"properties": {
"country": {"type": "string"},
"capital": {"type": "string"}
},
"required": ["country", "capital"],
"additionalProperties": False
}
}
}
)

print(response.choices[0].message.content)

Vision​

All Anthropic vision capabilities are supported by claude_code_native:

from litellm import completion
import os
import base64

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")

image_path = "../proxy/cached_logo.jpg"
base64_image = encode_image(image_path)

resp = litellm.completion(
model="claude_code_native/claude-3-opus-20240229",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Whats in this image?"},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64," + base64_image
},
},
],
}
],
)
print(f"\nResponse: {resp}")

MCP Tool Calling​

MCP tool calling is supported with the claude_code_native provider:

import os 
from litellm import completion

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

tools=[
{
"type": "mcp",
"server_label": "deepwiki",
"server_url": "https://mcp.deepwiki.com/mcp",
"require_approval": "never",
},
]

response = completion(
model="claude_code_native/claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Who won the World Cup in 2022?"}],
tools=tools
)

Hosted Tools​

The claude_code_native provider supports Anthropic's hosted tools (Computer, Text Editor, Web Search, Memory):

Computer Use​

from litellm import completion
import os

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

tools = [
{
"type": "computer_20241022",
"function": {
"name": "computer",
"parameters": {
"display_height_px": 100,
"display_width_px": 100,
"display_number": 1,
},
},
}
]
model = "claude_code_native/claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "Save a picture of a cat to my desktop."}]

resp = completion(
model=model,
messages=messages,
tools=tools,
)
print(resp)
from litellm import completion
import os

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

model = "claude_code_native/claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "What's the weather like today?"}]

resp = completion(
model=model,
messages=messages,
web_search_options={
"search_context_size": "medium",
"user_location": {
"type": "approximate",
"approximate": {
"city": "San Francisco",
},
}
}
)
print(resp)

Memory​

from litellm import completion
import os

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

tools = [{
"type": "memory_20250818",
"name": "memory"
}]

model = "claude_code_native/claude-sonnet-4-5-20250929"
messages = [{"role": "user", "content": "Please remember that my favorite color is blue."}]

response = completion(
model=model,
messages=messages,
tools=tools,
)
print(response)

Passing Extra Headers​

from litellm import completion
import os

os.environ["CLAUDE_CODE_API_KEY"] = "your-oauth-bearer-token"

messages = [{"role": "user", "content": "What is Claude Code Native?"}]
response = completion(
model="claude_code_native/claude-3-5-sonnet-20240620",
messages=messages,
extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"}
)
info

The provider automatically merges any extra headers with its required headers (anthropic-beta: oauth-2025-04-20 and anthropic-version: 2023-06-01). The provider's required headers take precedence.

Comparison with Anthropic Provider​

Featureanthropic/claude_code_native/
Authenticationx-api-key headerAuthorization: Bearer header
System PromptUser-provided onlyAuto: "You are Claude Code, Anthropic's official CLI for Claude." + User-provided
Required Headersanthropic-versionanthropic-version, anthropic-beta: oauth-2025-04-20
Use CaseGeneral purposeClaude Code CLI applications
Tool SupportFullFull (same as Anthropic)
Prompt CachingSupportedSupported
VisionSupportedSupported

API Reference​

For detailed API information, see:

note

The claude_code_native provider builds on top of the Anthropic provider implementation. All Anthropic-specific features and parameters are fully supported.