| 中文 | English |
This document provides detailed information about the Claude Code Adapter FastAPI API endpoints.
http://localhost:8000application/jsonChecks the health status of the service.
Response Example:
{
"ok": true,
"target_base": "http://127.0.0.1:1234"
}
Status Codes:
200 OK: Service is operationalProxies message requests to the target service, supporting tool calls.
Request Body:
{
"model": "string",
"messages": [
{
"role": "user|assistant|system",
"content": "string|object|array"
}
],
"max_tokens": 4096,
"temperature": 0.1,
"stream": false,
"system": "string",
"tools": [
{
"name": "string",
"description": "string",
"input_schema": {
"type": "object",
"properties": {},
"required": []
}
}
]
}
Response Examples:
Non-Streaming Response:
{
"id": "msg_1234567890",
"type": "message",
"role": "assistant",
"model": "target-model",
"stop_reason": "end_turn|tool_use",
"stop_sequence": null,
"usage": {
"input_tokens": 100,
"output_tokens": 50
},
"content": [
{
"type": "text",
"text": "Hello! How can I help you?"
}
]
}
Tool Call Response:
{
"id": "msg_1234567890",
"type": "message",
"role": "assistant",
"model": "target-model",
"stop_reason": "tool_use",
"stop_sequence": null,
"usage": {
"input_tokens": 100,
"output_tokens": 50
},
"content": [
{
"type": "text",
"text": "I'll help you with that."
},
{
"type": "tool_use",
"id": "call_123",
"name": "get_weather",
"input": {
"location": "Beijing"
}
}
]
}
Streaming Response:
data: {"id": "msg_123", "object": "chat.completion.chunk", "choices": [{"delta": {"content": "Hello"}}]}
data: {"id": "msg_123", "object": "chat.completion.chunk", "choices": [{"delta": {"content": "!"}}]}
data: [DONE]
Status Codes:
200 OK: Request successful400 Bad Request: Invalid request format500 Internal Server Error: Server error502 Bad Gateway: Target service errorThe system automatically selects the tool definition handling method based on configuration to optimize performance and functionality:
| Configuration | Tool Definition Location | Advantages | Use Cases |
|---|---|---|---|
enable_tool_selection: true |
Appended as user message | Avoids system prompt cache invalidation, improves performance | Dynamic tool selection, high-performance scenarios |
enable_tool_selection: false |
Included in system prompt | Ensures model awareness of all available tools, maintains functionality | Stable tool support, traditional scenarios |
enable_tool_selection settingtrue: Tool definitions → User messagesfalse: Tool definitions → System prompt{
"name": "tool_name",
"description": "Tool description",
"input_schema": {
"type": "object",
"properties": {
"param1": {
"type": "string",
"description": "Parameter description"
},
"param2": {
"type": "number",
"description": "Another parameter"
}
},
"required": ["param1"]
}
}
Tool call format in the model response:
{
"type": "tool_use",
"id": "call_123",
"name": "tool_name",
"input": {
"param1": "value1",
"param2": 42
}
}
curl -X POST "http://localhost:8000/v1/messages" \
-H "Content-Type: application/json" \
-d '{
"model": "test-model",
"messages": [
{
"role": "user",
"content": "Hello, how are you?"
}
]
}'
curl -X POST "http://localhost:8000/v1/messages" \
-H "Content-Type: application/json" \
-d '{
"model": "test-model",
"messages": [
{
"role": "user",
"content": "What is the weather like in Beijing?"
}
],
"tools": [
{
"name": "get_weather",
"description": "Get current weather information",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name"
}
},
"required": ["location"]
}
}
]
}'
curl -X POST "http://localhost:8000/v1/messages" \
-H "Content-Type: application/json" \
-d '{
"model": "test-model",
"messages": [
{
"role": "user",
"content": "Tell me a story"
}
],
"stream": true
}'
Input (Anthropic Format):
{
"model": "claude-3-sonnet",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"system": "You are a helpful assistant",
"tools": [
{
"name": "get_weather",
"description": "Get weather"
}
]
}
Converted (OpenAI Format):
When tool selection is disabled (enable_tool_selection: false):
{
"model": "claude-3-sonnet",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant\n\nYou have access to the following tools..."
},
{
"role": "user",
"content": "Hello"
}
]
}
When tool selection is enabled (enable_tool_selection: true):
{
"model": "claude-3-sonnet",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"content": "Hello"
},
{
"role": "user",
"content": "You have access to the following tools..."
}
]
}
Input (OpenAI Response):
{
"choices": [
{
"message": {
"content": "I'll help you with that.\n\n```json\n{\"type\": \"tool_use\", \"id\": \"call_123\", \"name\": \"get_weather\", \"input\": {\"location\": \"Beijing\"}}\n```"
}
}
]
}
Converted (Anthropic Format):
{
"content": [
{
"type": "text",
"text": "I'll help you with that."
},
{
"type": "tool_use",
"id": "call_123",
"name": "get_weather",
"input": {
"location": "Beijing"
}
}
],
"stop_reason": "tool_use"
}
{
"detail": "Error message description"
}
| Status Code | Error Type | Description |
|---|---|---|
| 400 | Bad Request | Invalid request format |
| 500 | Internal Server Error | Server internal error |
| 502 | Bad Gateway | Target service unavailable |
{
"detail": "invalid json body"
}
{
"detail": "request failed: Connection refused"
}