litellm/litellm/anthropic_interface
Mateo Wang 5084cef13c
refactor(typing): clear 1146 basedpyright Any errors across 365 files (#52768)
* refactor(typing): clear basedpyright Any errors in proxy, router and core utils

* refactor(typing): clear basedpyright Any errors in proxy server, logging and provider handlers

* refactor(typing): clear basedpyright Any errors in types, proxy client and single-Any modules

* refactor(typing): clear basedpyright Any errors in providers, responses and proxy endpoints

* refactor(typing): clear basedpyright Any errors in key management, prometheus, gigachat and http handler

* refactor(typing): clear basedpyright Any errors in a long-tail family sweep

* refactor(typing): clear basedpyright Any errors in decode, guardrail and passthrough paths

* refactor(typing): clear basedpyright Any errors in decode, realtime and management paths

* refactor(typing): clear basedpyright Any errors in source-owned proxy, cache and provider paths

* refactor(typing): clear basedpyright Any errors in wave 10 narrow deep pass

* refactor(typing): clear basedpyright Any errors in wave 11 hotspot and route request pass

* refactor(typing): clear basedpyright Any errors in wave 12 deep coupled pass

* refactor(typing): clear basedpyright Any errors in wave 13 family pass

* refactor(typing): clear basedpyright Any errors in wave 14 single-hotspot pass

* refactor(typing): clear basedpyright Any errors in wave 15 single-hotspot pass

* fix(typing): keep main's merged behavior typed after syncing with main

* refactor(typing): share one int_of helper across batch, file and key endpoints

* test: give mocked Anthropic calls a real message body

The Anthropic chat transform now rejects a decoded body that is not a JSON
object. Four tests mocked the HTTP client with a bare MagicMock, whose json()
is never a real decoded response, so they now return a real Anthropic message.

* fix(utils): keep FileTypes importable from litellm.utils

The wave 14 pass dropped the now-unused FileTypes import, which also removed the litellm.utils.FileTypes attribute SDK users could import. Re-export it as a TypeAlias the same way TextChoices is.

* fix: run real builtins instead of copied CPython errors and keep client SDK types

Replace hand-copied CPython error sites with the real operations through
small core_helpers wrappers, restore main's typed client SDK returns, and
add tests that compare each helper's result and error with the builtin's

* fix: raise instead of recursing in the typed narrowing helpers

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: call the value directly so the helpers test collects on Python 3.10

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor(typing): mark the three widened TypedDict fields read-only

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-21 04:25:33 +00:00
..
exceptions refactor(typing): clear 1146 basedpyright Any errors across 365 files (#52768) 2026-11-21 04:25:33 +00:00
messages refactor(typing): clear 1143 basedpyright Any errors across 273 files (#46574) 2026-10-15 21:08:47 +00:00
__init__.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
readme.md fix(anthropic): make opening usage estimates opt-in (#48633) 2026-10-27 12:57:21 -07:00

Use LLM API endpoints in Anthropic Interface

Note: This is called anthropic_interface because anthropic is a known python package and was failing mypy type checking.

Usage


LiteLLM Python SDK

Non-streaming example

import litellm
response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    api_key=api_key,
    model="anthropic/claude-3-haiku-20240307",
    max_tokens=100,
)

Example response:

{
  "content": [
    {
      "text": "Hi! this is a very short joke",
      "type": "text"
    }
  ],
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "model": "claude-3-7-sonnet-20250219",
  "role": "assistant",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 2095,
    "output_tokens": 503,
    "cache_creation_input_tokens": 2095,
    "cache_read_input_tokens": 0
  }
}

Streaming example

import litellm
response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    api_key=api_key,
    model="anthropic/claude-3-haiku-20240307",
    max_tokens=100,
    stream=True,
)
async for chunk in response:
    print(chunk)

Optional opening token estimate

When /v1/messages streams through the Chat Completions adapter, the opening message_start.usage.input_tokens defaults to 0. To show an estimated prompt size while a response streams, enable anthropic_messages_estimate_initial_input_tokens. This setting defaults to false and applies to all requests using this adapter

For an existing proxy deployment, add the setting under litellm_settings in config.yaml and restart the proxy. Use a YAML boolean, without quotes

litellm_settings:
  anthropic_messages_estimate_initial_input_tokens: true

For the Python SDK, set the option before making streaming Messages requests

import litellm

litellm.anthropic_messages_estimate_initial_input_tokens = True

Send a streaming Messages request to a Chat Completions-backed model, such as a hosted_vllm/ model. The opening message_start event includes the estimated input count. If estimation fails, the count remains 0. Set the option to false in YAML or False in Python to disable estimation and skip token counting

The opening count is approximate and is not suitable for billing or enforcing exact context limits. Opening output and cache counts remain 0. Final message_delta usage still comes from the provider and takes precedence over the opening estimate. Native Anthropic streams, the Responses adapter, and non-streaming responses are unaffected. This setting does not select the adapter

LiteLLM Proxy Server

  1. Setup config.yaml
model_list:
    - model_name: anthropic-claude
      litellm_params:
        model: claude-3-7-sonnet-latest
  1. Start proxy
litellm --config /path/to/config.yaml
  1. Test it!
import anthropic

# point anthropic sdk to litellm proxy 
client = anthropic.Anthropic(
    base_url="http://0.0.0.0:4000",
    api_key="<your-master-key>",
)

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="anthropic/claude-3-haiku-20240307",
    max_tokens=100,
)
curl -L -X POST 'http://0.0.0.0:4000/v1/messages' \
-H 'content-type: application/json' \
-H 'x-api-key: $LITELLM_API_KEY' \
-H 'anthropic-version: 2023-06-01' \
-d '{
  "model": "anthropic-claude",
  "messages": [
    {
      "role": "user",
      "content": "Hello, can you tell me a short joke?"
    }
  ],
  "max_tokens": 100
}'