Claude’s context window is like a scratchpad for your conversation, but unlike a physical scratchpad, it has a strict token limit. Stuffing too much in there means Claude might forget earlier parts of the conversation or, worse, simply refuse to process the prompt. The trick is to get the right information in, not all the information.
Here’s Claude in action, processing a prompt with injected context. Imagine we’re building a personalized travel itinerary generator.
# Simulate a user's preferences and past trips
user_profile = {
"name": "Alex",
"preferred_destinations": ["Kyoto", "Rome", "Banff"],
"travel_style": "historical sites, local cuisine, moderate pace",
"past_trips": [
{"destination": "Kyoto", "activities": ["Fushimi Inari Shrine", "Gion District", "Kiyomizu-dera Temple"]},
{"destination": "Paris", "activities": ["Eiffel Tower", "Louvre Museum", "Sainte-Chapelle"]}
],
"budget_level": "mid-range"
}
# Simulate Claude's knowledge base about a destination
destination_info = {
"Kyoto": {
"key_attractions": ["Kinkaku-ji (Golden Pavilion)", "Arashiyama Bamboo Grove", "Nijo Castle"],
"cuisine_highlights": ["Kaiseki ryori", "Matcha sweets", "Yudofu"],
"transportation": "Efficient bus and subway system, walking is pleasant in many areas."
}
}
# Construct the prompt, injecting relevant context
# We'll selectively pull from the user_profile and destination_info
prompt_template = """
You are a travel assistant. Plan a 3-day itinerary for Alex in Kyoto.
Alex's preferences: {user_preferences}.
Alex has visited: {past_trips_summary}.
Kyoto details: {kyoto_details}.
Focus on historical sites and local cuisine, with a moderate pace.
Plan the itinerary day by day.
"""
# Process and inject context
user_preferences_str = f"preferred destinations: {', '.join(user_profile['preferred_destinations'])}; travel style: {user_profile['travel_style']}; budget: {user_profile['budget_level']}"
past_trips_summary_str = ", ".join([trip['destination'] for trip in user_profile['past_trips']])
kyoto_details_str = f"key attractions: {', '.join(destination_info['Kyoto']['key_attractions'])}; cuisine highlights: {', '.join(destination_info['Kyoto']['cuisine_highlights'])}; transportation: {destination_info['Kyoto']['transportation']}"
final_prompt = prompt_template.format(
user_preferences=user_preferences_str,
past_trips_summary=past_trips_summary_str,
kyoto_details=kyoto_details_str
)
print(final_prompt)
This prompt is far more effective than just saying "Plan a trip to Kyoto for Alex." We’ve provided Claude with Alex’s specific tastes, where they’ve been (to avoid repetition or suggest complementary experiences), and key facts about Kyoto.
The core problem this solves is information relevance. A massive context window is useless if it’s filled with noise. By carefully selecting and formatting information, we ensure Claude has the data it needs to generate a high-quality, personalized response without exceeding its operational limits.
Internally, Claude processes this prompt by first tokenizing it. Each word, punctuation mark, and even space is assigned a numerical token. The model then uses these tokens as input for its attention mechanisms, which determine how much "weight" or importance each token should have when generating its output. By pre-processing and summarizing information, we are essentially creating more "dense" tokens that convey more meaning, rather than "sparse" tokens that might represent a single, less impactful piece of data.
The levers you control are:
- Summarization: Instead of dumping the entire
user_profiledictionary, we extract and format key details. "Alex has visited: Kyoto, Paris" is far more token-efficient than listing all activities for each past trip. - Key-Value Formatting: Using formats like "preferred destinations: Kyoto, Rome, Banff" makes the information scannable and structured for Claude, akin to providing a small, structured database within the prompt.
- Conditional Inclusion: You can dynamically decide what information is relevant. If Alex’s
budget_levelwas "luxury," you might include different Kyoto details than for "budget-conscious." - Conciseness: Phrasing matters. "Moderate pace" uses fewer tokens than "Alex prefers not to rush and likes to have time to savor each experience."
The one thing most people don’t realize is that the order of information, and how you structure it using simple delimiters (like colons, commas, or even markdown lists), can significantly impact Claude’s ability to extract and utilize that context efficiently. Claude isn’t just reading words; it’s parsing structure. Providing that structure yourself, even in plain text, helps it build a more accurate internal representation of the data you’ve given it.
The next step is to explore techniques for dynamically updating this context as the conversation progresses.