The Claude AI Community Discord is open.Join us

Making API Calls

10 min readYour First App

Now that your environment is set up, let's explore the Messages API in depth.

The Messages API

The Messages API is the primary way to interact with Claude. Every request requires:

  • model — which Claude model to use
  • max_tokens — maximum response length
  • messages — conversation history

Basic Request

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain quantum computing in simple terms"}
    ]
)

print(response.content[0].text)

Multi-Turn Conversations

To have a back-and-forth conversation, include the full message history:

messages = [
    {"role": "user", "content": "What is Python?"},
    {"role": "assistant", "content": "Python is a high-level programming language..."},
    {"role": "user", "content": "What makes it good for AI?"}
]

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=messages
)

System Prompts

Use system prompts to set Claude's behavior:

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a helpful coding tutor. Explain concepts clearly with examples.",
    messages=[{"role": "user", "content": "How do decorators work?"}]
)

Error Handling

Always handle potential errors:

try:
    response = client.messages.create(...)
except anthropic.RateLimitError:
    print("Rate limited — wait and retry")
except anthropic.APIError as e:
    print(f"API error: {e}")