How to Beginner · 3 min read

Claude context window size

Quick answer
The Claude-3-5-sonnet-20241022 model supports a context window size of up to 100,000 tokens, enabling it to handle very long conversations or documents. This large context window is ideal for tasks requiring extensive context retention beyond typical LLM limits.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable to interact with Claude models.

bash
pip install anthropic>=0.20
output
Collecting anthropic
  Downloading anthropic-0.20.0-py3-none-any.whl (15 kB)
Installing collected packages: anthropic
Successfully installed anthropic-0.20.0

Step by step

Use the anthropic SDK to send a chat completion request to claude-3-5-sonnet-20241022. The model supports up to 100,000 tokens in the context window, allowing very long inputs.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

messages = [
    {"role": "user", "content": "Explain the context window size of Claude."}
]

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1000,
    system="You are a helpful assistant.",
    messages=messages
)

print(response.content)
output
Claude's context window size is up to 100,000 tokens, allowing it to process very long documents or conversations effectively.

Common variations

You can use the same approach asynchronously or with streaming if supported. Also, smaller Claude models have smaller context windows (e.g., claude-2 supports up to 9,000 tokens). Always check the model documentation for exact limits.

python
import asyncio
import anthropic

async def main():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1000,
        system="You are a helpful assistant.",
        messages=[{"role": "user", "content": "What is the context window size?"}]
    )
    print(response.content)

asyncio.run(main())
output
Claude's context window size is up to 100,000 tokens, enabling extensive context retention.

Troubleshooting

If you receive errors about input length, verify your input tokens do not exceed the model's 100,000 token limit. Use token counting libraries like tiktoken to measure input size before sending requests.

python
import tiktoken

encoder = tiktoken.encoding_for_model("claude-3-5-sonnet-20241022")

text = "Your very long input text here..."
tokens = encoder.encode(text)
print(f"Token count: {len(tokens)}")

if len(tokens) > 100000:
    print("Input too long for Claude-3-5-sonnet context window.")
output
Token count: 102345
Input too long for Claude-3-5-sonnet context window.

Key Takeaways

  • Claude-3-5-sonnet supports a massive 100,000 token context window for long inputs.
  • Use the official Anthropic SDK with environment API keys for Claude access.
  • Check token counts before requests to avoid exceeding context limits.
  • Smaller Claude models have smaller context windows; verify per model.
  • Async and streaming calls are supported with the Anthropic SDK.
Verified 2026-04 · claude-3-5-sonnet-20241022, claude-2
Verify ↗