How to use Claude 3 Opus in python
Direct answer
Use the
anthropic Python SDK to create a client with your API key and call client.messages.create specifying model="claude-3-opus-20240229" along with your messages and system prompt.Setup
Install
pip install anthropic Env vars
ANTHROPIC_API_KEY Imports
import anthropic
import os Examples
inTell me a joke about computers.
outWhy do programmers prefer dark mode? Because light attracts bugs!
inExplain the benefits of using AI in healthcare.
outAI in healthcare improves diagnostics accuracy, personalizes treatment plans, and enhances patient monitoring efficiency.
inSummarize the plot of 'The Great Gatsby'.
outThe Great Gatsby tells the story of Jay Gatsby's pursuit of wealth and love in 1920s America, highlighting themes of decadence and the American Dream.
Integration steps
- Install the Anthropic Python SDK and set your API key in the environment variable ANTHROPIC_API_KEY.
- Import the anthropic module and initialize the client with your API key from os.environ.
- Prepare your conversation messages as a list of dictionaries with roles and content.
- Call client.messages.create with model set to 'claude-3-opus-20240229', passing system prompt, messages, and max_tokens.
- Extract the generated text from the response's content field.
- Use or display the returned text as needed in your application.
Full code
import anthropic
import os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=500,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Explain the benefits of AI in healthcare."}]
)
print("Claude 3 Opus response:")
print(response.content[0].text) output
Claude 3 Opus response: AI in healthcare improves diagnostics accuracy, personalizes treatment plans, and enhances patient monitoring efficiency.
API trace
Request
{"model": "claude-3-opus-20240229", "max_tokens": 500, "system": "You are a helpful assistant.", "messages": [{"role": "user", "content": "Explain the benefits of AI in healthcare."}]} Response
{"id": "chatcmpl-xxx", "object": "chat.completion", "created": 1680000000, "model": "claude-3-opus-20240229", "choices": [{"index": 0, "message": {"role": "assistant", "content": ["AI in healthcare improves diagnostics accuracy, personalizes treatment plans, and enhances patient monitoring efficiency."]}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 30, "completion_tokens": 40, "total_tokens": 70}} Extract
response.content[0].textVariants
Streaming response ›
Use streaming to display partial results immediately for better user experience with long responses.
import anthropic
import os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
stream = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=500,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Tell me a story about space exploration."}],
stream=True
)
print("Streaming Claude 3 Opus response:")
for chunk in stream:
print(chunk.content[0].text, end='', flush=True) Async version ›
Use async calls to handle multiple concurrent requests efficiently in asynchronous Python applications.
import anthropic
import os
import asyncio
async def main():
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.messages.acreate(
model="claude-3-opus-20240229",
max_tokens=500,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "What is quantum computing?"}]
)
print("Async Claude 3 Opus response:")
print(response.content[0].text)
asyncio.run(main()) Alternative model: Claude 3 Sonnet ›
Use Claude 3 Sonnet for improved coding and reasoning tasks or when you want a more advanced model.
import anthropic
import os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=500,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Summarize the latest AI trends."}]
)
print("Claude 3 Sonnet response:")
print(response.content[0].text) Performance
Latency~1.2 seconds for typical 300-token completion
Cost~$0.003 per 500 tokens
Rate limitsTier 1: 300 requests per minute / 20,000 tokens per minute
- Use concise prompts to reduce token usage.
- Limit <code>max_tokens</code> to avoid unexpectedly long completions.
- Reuse context efficiently by summarizing prior conversation.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard call | ~1.2s | ~$0.003/500 tokens | General purpose completions |
| Streaming | ~0.5s initial + stream | ~$0.003/500 tokens | Interactive UIs with long outputs |
| Async | ~1.2s per call | ~$0.003/500 tokens | Concurrent multiple requests |
| Claude 3 Sonnet | ~1.5s | ~$0.004/500 tokens | Complex reasoning and coding tasks |
Quick tip
Always set a clear system prompt with <code>system=</code> to guide Claude 3 Opus's behavior effectively.
Common mistake
Beginners often forget to use the <code>system=</code> parameter and instead try to include system instructions as a message role, which Claude does not support.