Article·anthropic.com
AILLMAnthropicaillmanthropicapipythonclaude
Claude 4 Opus
Quickly access and leverage Anthropic's most capable AI model, Claude 3 Opus, for advanced reasoning and superior coding tasks. This Action Pack guides you through setting up the API and making your first powerful call.
beginner15 min5 steps
The play
- Acquire an Anthropic API KeyVisit the Anthropic console (console.anthropic.com), sign up or log in, and navigate to API Keys to generate a new key. This key is essential for authenticating your requests.
- Install the Anthropic Python SDKOpen your terminal or command prompt and install the official Anthropic Python SDK using pip. This SDK simplifies interaction with the Claude API.
- Set Your API Key Environment VariableBefore running your script, securely store your API key as an environment variable named `ANTHROPIC_API_KEY`. This prevents hardcoding sensitive information.
- Write Your First Interaction ScriptCreate a Python file (e.g., `claude_opus_chat.py`). Import the `Anthropic` client, initialize it using your environment variable, and construct a simple message request to Claude 3 Opus, Anthropic's current most capable model.
- Execute the Script and Review OutputRun your Python script from the terminal. Observe Claude's generated explanation of quantum entanglement. You can modify the `content` in the script for different prompts.
Starter code
import os
from anthropic import Anthropic
# Ensure ANTHROPIC_API_KEY environment variable is set
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
message = client.messages.create(
model="claude-3-opus-20240229", # Anthropic's current most capable model
max_tokens=1024,
messages=[
{"role": "user", "content": "Write a Python function that calculates the factorial of a number using recursion."}
]
)
print(message.content)Source