What You’ll Learn
By the end of this guide, you’ll understand:- How conversation context works in Chatbot Platform
- Configuring context window sizes
- Managing conversation history effectively
- When to clear or preserve context
- Per-user vs per-channel context handling
- Advanced memory management patterns
- Context optimization for cost and performance
Prerequisites
Before starting, you should:- Have a working bot with at least one channel
- Understand basic bot concepts from Support Bot Basics
- Know how webhook integrations work
- Have some experience with chatbot conversations
How Conversation Context Works
The Basics
Every time a user sends a message, your bot needs context to provide relevant responses:Context Window
The context window determines how many previous messages are included when sending requests to your AI integration:Why Context Matters
Good Context:- Bot remembers what you’re talking about
- No need to repeat information
- Natural, flowing conversation
- Better user experience
- Bot asks for information you already provided
- Doesn’t understand references (“it”, “that”, “them”)
- Repetitive or confused responses
- Frustrating user experience
Step 1: Configure Context Window
1
Go to Bot Settings
Navigate to your bot and click the Settings or Behavior tab.
2
Set Context Window
Configure the number of messages to include:
- Small (5-10 messages): Quick questions, minimal context needed
- Medium (10-20 messages): Standard support conversations
- Large (20-30 messages): Complex discussions, detailed troubleshooting
- Very Large (30+ messages): Long-form consultations, extended sessions
Larger context windows cost more (more tokens sent to AI) but provide better context.
3
Consider Token Costs
Calculate approximate costs:
4
Test Different Sizes
Try different window sizes:
- Start with 15 messages
- Monitor conversation quality
- Check token usage
- Adjust up or down based on needs
Step 2: Understanding Per-User Context
Context is maintained per user and per channel:Same User, Different Channels
Different Users, Same Channel
Platform-Specific Behavior
Telegram:- Per-user context in groups
- Separate context for each private chat
- Group messages don’t mix with private chats
- Thread-based context in channels
- Per-user context in DMs
- Different threads = different contexts
- Per-user context in servers
- Thread-based in thread channels
- DM context separate from server context
Step 3: Managing Conversation History
When to Clear Context
Clear conversation context when: User Explicitly Requests:How to Clear Context
Via API:Clear conversation context
Clear conversation context
- cURL
- JavaScript
- PHP
- Python
When to Preserve Context
Keep conversation context when: Follow-up Questions:Advanced Context Patterns
Pattern 1: Sliding Window with Summarization
For very long conversations, summarize older messages:Pattern 2: Contextual Memory Injection
Inject relevant context from external sources:Pattern 3: Topic-Based Context Segmentation
Maintain separate contexts for different topics:Pattern 4: Selective Context Filtering
Remove irrelevant messages from context:Context Optimization
Reducing Token Usage
Strategy 1: Compress System PromptsImproving Response Quality
Strategy 1: Include Structured ContextAPI Examples
Get conversation history
Get conversation history
- cURL
- JavaScript
- PHP
- Python
Add context to conversation
Add context to conversation
- cURL
- JavaScript
- PHP
- Python
Search conversation history
Search conversation history
- cURL
- JavaScript
- PHP
- Python
Best Practices
Right-Size Windows
Use appropriate context window for your use case
Monitor Costs
Track token usage and optimize for cost
Clear When Needed
Don’t let stale context confuse the bot
Enrich Context
Add relevant external data to improve responses
Context Window Guidelines
System Prompt Best Practices
Include context handling instructions in your system prompt:Troubleshooting
Bot doesn’t remember earlier messages
Possible causes:- Context window too small
- Conversation history cleared accidentally
- Integration not receiving full message array
- Increase context window size
- Check conversation history via API
- Verify webhook payload includes
messagesarray - Review integration logs
Bot confused by old context
Possible causes:- Topic changed but old context still included
- Context window too large
- Multiple unrelated topics in same conversation
- Implement topic detection and context clearing
- Reduce context window size
- Add context filtering logic
- Provide clear conversation restart mechanism
High token costs
Possible causes:- Context window too large
- Verbose system prompts
- Not filtering irrelevant messages
- Reduce context window
- Compress system prompts
- Implement message filtering
- Use cheaper models for simple queries
- Add summarization for long conversations
Different context per channel
Expected behavior: Each channel maintains separate context If you need shared context:- Use external database to store cross-channel history
- Implement custom context loading in integration
- Reference Conversation Archives MCP server
- Link user IDs across platforms
Next Steps
Build Grounded Bot
Add external data sources to context
Personal Assistant
Advanced context with multiple tools
Conversation API
Programmatic history management
Message History
View and analyze conversations
Related Documentation
- Conversations Overview - Conversation management concepts
- Message History - Viewing conversation logs
- Conversation API - API reference for conversations
- Conversation Archives MCP - Search past conversations with AI
- API Reference - Complete API documentation