Basic interaction with Anthropic Claude models
This example demonstrates how to set up an Anthropic Claude model and send a basic user prompt with Python.
Requirements
- Python 3.6+
- An Anthropic API key
Steps for running this example in the shell
- Install dependencies:
bash
python -m venv .venv
# macOS/Linux:
source .venv/bin/activate
# Windows Command Prompt:
.venv\Scripts\activate.bat
# Windows PowerShell:
.venv\Scripts\Activate.ps1
pip install -r requirements.txt- Export your Anthropic API key as an environment variable:
bash
export ANTHROPIC_API_KEY="sk-..." # Windows cmd: set ANTHROPIC_API_KEY="sk-..." # Windows PowerShell: $env:ANTHROPIC_API_KEY="sk-..."- Run the script:
bash
python anthropic-claude-basic.pyOutput
When you run the script, it will send a user prompt to a Claude model (claude-haiku-4-5-20251001). Then, it will send the same user prompt to a more advanced model (claude-sonnet-4-6) using extended thinking. The output will show the responses from both models. The transcript below was captured with earlier releases of these models. Model identifiers, latency, token counts, and wording will differ on each run.
=== Basic model ===
User: How many tokens are in your context window?
Model: claude-3-haiku-20240307
Latency: 3.908 seconds
Input tokens: 15
Output tokens: 73
Claude3: I do not actually have a fixed context window size. I am an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't have the same architectural details as language models that use a sliding context window. My responses are generated based on my training by Anthropic, not a fixed-size input context.
=== Advanced model ===
User: How many tokens are in your context window?
Model: claude-sonnet-4-20250514
Latency: 5.787 seconds
Input tokens: 44
Output tokens: 221
Claude4: I don't have definitive information about my exact context window size. Different versions of Claude have had different context window sizes, and I'm not certain which specific configuration I'm running on or what my current limits are.
If you're working on something that might approach context limits, I'd be happy to help you work within whatever constraints we encounter. Is there something specific you're trying to do that requires knowing the context window size?