/v1/messages

/v1/messages

LiteLLM을 사용해 모든 LLM API를 Anthropic v1/messages 형식으로 호출할 수 있어요.

개요

기능 지원 비고
비용 추적 (Cost Tracking) 모든 지원 모델에서 동작
로깅 (Logging) 모든 통합에서 동작
최종 사용자 추적 (End-user Tracking)
스트리밍 (Streaming)
폴백 (Fallbacks) 지원 모델 간 동작
로드 밸런싱 (Loadbalancing) 지원 모델 간 동작
가드레일 (Guardrails) 입력·출력 텍스트에 적용 (비스트리밍 전용)
지원 프로바이더 모든 LiteLLM 지원 프로바이더 openai, anthropic, bedrock, vertex_ai, gemini, azure, azure_ai

사용법

LiteLLM Python SDK

  • Anthropic
  • OpenAI
  • Google AI Studio
  • Vertex AI
  • AWS Bedrock

비스트리밍 예시

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-sonnet-5",
    max_tokens=100,
)

스트리밍 예시

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-sonnet-5",
    max_tokens=100,
    stream=True,
)
async for chunk in response:
    print(chunk)

비스트리밍 예시 (OpenAI)

import litellm
import os

# Set API key
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"

response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="openai/gpt-5.6-terra",
    max_tokens=100,
)

스트리밍 예시 (OpenAI)

import litellm
import os

# Set API key
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"

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

비스트리밍 예시 (Google AI Studio)

import litellm
import os

# Set API key
os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="gemini/gemini-3.8-flash",
    max_tokens=100,
)

스트리밍 예시 (Google AI Studio)

import litellm
import os

# Set API key
os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

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

비스트리밍 예시 (Vertex AI)

import litellm
import os

# Set credentials - Vertex AI uses application default credentials
# Run 'gcloud auth application-default login' to authenticate
os.environ["VERTEXAI_PROJECT"] = "your-gcp-project-id"
os.environ["VERTEXAI_LOCATION"] = "us-central1"

response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="vertex_ai/gemini-3.8-flash",
    max_tokens=100,
)

스트리밍 예시 (Vertex AI)

import litellm
import os

# Set credentials - Vertex AI uses application default credentials
# Run 'gcloud auth application-default login' to authenticate
os.environ["VERTEXAI_PROJECT"] = "your-gcp-project-id"
os.environ["VERTEXAI_LOCATION"] = "us-central1"

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

비스트리밍 예시 (AWS Bedrock)

import litellm
import os

# Set AWS credentials
os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key-id"
os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-access-key"
os.environ["AWS_REGION_NAME"] = "us-west-2"  # or your AWS region

response = await litellm.anthropic.messages.acreate(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="bedrock/us.anthropic.claude-sonnet-5",
    max_tokens=100,
)

스트리밍 예시 (AWS Bedrock)

import litellm
import os

# Set AWS credentials
os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key-id"
os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-access-key"
os.environ["AWS_REGION_NAME"] = "us-west-2"  # or your AWS region

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

예시 응답:

{
  "content": [
    {
      "text": "Hi! this is a very short joke",
      "type": "text"
    }
  ],
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "model": "claude-sonnet-5",
  "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
  }
}

LiteLLM Proxy Server

  • Anthropic
  • OpenAI
  • Google AI Studio
  • Vertex AI
  • AWS Bedrock
  • curl
  1. config.yaml 설정
model_list:
    - model_name: anthropic-claude
      litellm_params:
        model: claude-sonnet-5
        api_key: os.environ/ANTHROPIC_API_KEY
  1. 프록시 시작
litellm --config /path/to/config.yaml
  1. 테스트!
import anthropic

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

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="anthropic-claude",
    max_tokens=100,
)

OpenAI의 경우:

model_list:
    - model_name: openai-gpt4
      litellm_params:
        model: openai/gpt-5.6-terra
        api_key: os.environ/OPENAI_API_KEY
import anthropic

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

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="openai-gpt4",
    max_tokens=100,
)

Google AI Studio의 경우:

model_list:
    - model_name: gemini-2-flash
      litellm_params:
        model: gemini/gemini-3.8-flash
        api_key: os.environ/GEMINI_API_KEY
import anthropic

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

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="gemini-2-flash",
    max_tokens=100,
)

Vertex AI의 경우:

model_list:
    - model_name: vertex-gemini
      litellm_params:
        model: vertex_ai/gemini-3.8-flash
        vertex_project: your-gcp-project-id
        vertex_location: us-central1
import anthropic

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

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="vertex-gemini",
    max_tokens=100,
)

AWS Bedrock의 경우:

model_list:
    - model_name: bedrock-claude
      litellm_params:
        model: bedrock/us.anthropic.claude-sonnet-5
        aws_access_key_id: os.environ/AWS_ACCESS_KEY_ID
        aws_secret_access_key: os.environ/AWS_SECRET_ACCESS_KEY
        aws_region_name: us-west-2
import anthropic

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

response = client.messages.create(
    messages=[{"role": "user", "content": "Hello, can you tell me a short joke?"}],
    model="bedrock-claude",
    max_tokens=100,
)

curl로:

curl -L -X POST 'http://0.0.0.0:4000/v1/messages' \
-H 'content-type: application/json' \
-H "x-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
}'

요청 형식

요청 본문은 Anthropic messages API 형식이에요. LiteLLM은 이 엔드포인트에 대해 Anthropic messages 스펙을 따릅니다.

예시 요청 본문

{
  "model": "claude-sonnet-5",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "Hello, world"
    }
  ]
}

필수 필드

  • model (string): 모델 식별자 (예: "claude-sonnet-5")
  • max_tokens (integer): 중단하기 전에 생성할 최대 토큰 수. 모델이 이 한도 전에 멈출 수 있으며, 값은 1보다 커야 해요.
  • messages (객체 배열): 대화 턴의 순서 있는 목록. 각 메시지 객체는 다음을 포함해야 합니다: role (enum: "user" 또는 "assistant"): 메시지의 화자를 지정. content (string 또는 content block 배열): 메시지를 이루는 텍스트 또는 content blocks (예: type"text"인 객체를 담은 배열). 예시 동등성: { "role": "user", "content": "Hello, Claude" }{ "role": "user", "content": [ { "type": "text", "text": "Hello, Claude" } ] }와 동등합니다.

선택 필드

  • metadata (object): 요청에 대한 추가 메타데이터 (예: user_id를 불투명 식별자로)
  • stop_sequences (string 배열): 생성된 텍스트에서 만나면 모델을 멈추게 하는 커스텀 시퀀스
  • stream (boolean): server-sent events로 응답을 스트리밍할지 여부
  • system (string 또는 배열): 모델에 컨텍스트나 특정 지침을 제공하는 시스템 프롬프트
  • temperature (number): 모델 응답의 무작위성 제어. 유효 범위: 0 < temperature < 1
  • thinking (object): 확장 사고(extended thinking)를 활성화하기 위한 구성. 활성화되면 포함: budget_tokens (integer): 최소 1024 토큰 (max_tokens보다 작아야 함). type (enum): 예: "enabled". summary (string, 선택): thinking 블록에 대한 summary 스타일 활성화. 가능한 값: "auto", "concise", "detailed", "disabled". Anthropic이 아닌 프로바이더(예: openai/gpt-5.6-terra)로 라우팅할 때는 summary 값이 보존되어 다운스트림 API로 전달됩니다.
  • tool_choice (object): 제공된 도구를 모델이 어떻게 사용해야 하는지 지시
  • tools (객체 배열): 모델이 사용할 수 있는 도구 정의. 각 도구는: name (string): 도구 이름. description (string): 도구의 상세 설명. input_schema (object): 도구의 예상 입력 형식을 설명하는 JSON 스키마.
  • top_k (integer): 상위 K 옵션으로 샘플링 제한
  • top_p (number): 누적 확률 컷오프로 nucleus sampling 활성화. 유효 범위: 0 < top_p < 1

응답 형식

응답은 Anthropic messages API 형식이에요.

예시 응답

{
  "content": [
    {
      "text": "Hi! My name is Claude.",
      "type": "text"
    }
  ],
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "model": "claude-sonnet-5",
  "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
  }
}

응답 필드

  • content (객체 배열): 모델이 생성한 콘텐츠 블록을 포함. 각 블록은: type (string): 콘텐츠 유형 (예: "text", "tool_use", "thinking", "redacted_thinking"). text (string): 모델이 생성한 텍스트. 최대 길이 5,000,000 문자. citations (객체 배열 또는 null): 인용 세부 정보를 제공하는 선택 필드. 각 인용은: cited_text, document_index, document_title (또는 null), start_char_index, end_char_index, type (보통 "char_location").
  • id (string): 응답 메시지의 고유 식별자. ID의 형식과 길이는 시간이 지나며 바뀔 수 있어요.
  • model (string): 응답을 생성한 모델
  • role (string): 생성된 메시지의 역할. 응답에서는 항상 "assistant".
  • stop_reason (string): 모델이 텍스트 생성을 멈춘 이유. 가능한 값: "end_turn": 자연스러운 중단점 도달. "max_tokens": 최대 토큰 한도 도달. "stop_sequence": 커스텀 stop sequence 만남. "tool_use": 모델이 하나 이상의 도구 호출.
  • stop_sequence (string 또는 null): 생성 중단을 유발한 특정 stop sequence. 해당 없으면 null.
  • type (string): 응답 객체 유형, 항상 "message".
  • usage (object): 청구와 속도 제한을 위한 토큰 사용량. input_tokens (integer): 처리된 총 입력 토큰. output_tokens (integer): 생성된 총 출력 토큰. cache_creation_input_tokens (integer 또는 null): 캐시 항목 생성에 사용된 토큰 수. cache_read_input_tokens (integer 또는 null): 캐시에서 읽은 토큰 수.

출처: 문서

더 알아보기 (Learn more)