context() method is a powerful feature that retrieves formatted conversation context from sessions, making it easy to integrate with LLMs like OpenAI, Anthropic, and others. This guide covers everything you need to know about working with session context.
By default, the context includes a blend of summary and messages which covers the entire session history of a peer.
Basic Usage
Thecontext() method is available on all Session objects and returns a SessionContext that contains the formatted conversation history.
Context Parameters
Thecontext() method accepts several optional parameters to customize the retrieved context:
Token Limits
Control the size of the context by setting a maximum token count:Summary Mode
Enable summary mode (on by default) to get a condensed version of the conversation:Peer Representation in Context
You can include a peer’s representation and peer card in the context by specifyingpeer_target. This is useful for providing the LLM with knowledge about a specific peer.
Semantic Search
Usesearch_query to fetch semantically relevant conclusions based on a query string (requires peer_target):
Session-Scoped Representations
Uselimit_to_session to only include conclusions from the current session:
All Parameters Reference
Converting to LLM Formats
TheSessionContext object provides methods to convert the context into formats compatible with popular LLM APIs. When converting to OpenAI format, you must specify the assistant peer to format the context in such a way that the LLM can understand it.
OpenAI Format
Convert context to OpenAI’s chat completion format:Anthropic Format
Convert context to Anthropic’s Claude format:Complete LLM Integration Examples
Using with OpenAI
Multi-Turn Conversation Loop
Advanced Context Usage
Context with Summaries for Long Conversations
For very long conversations, use summaries to maintain context while controlling token usage:Context for Different Assistant Types
You can get context formatted for different types of assistants in the same session:Best Practices
1. Token Management
Always set appropriate token limits to control costs and ensure context fits within LLM limits:2. Context Caching
For applications with frequent context retrieval, consider caching context when appropriate:3. Error Handling
Always handle potential errors when retrieving context:Conclusion
Thecontext() method is essential for integrating Honcho sessions with LLMs. By understanding how to:
- Retrieve context with appropriate parameters
- Convert context to LLM-specific formats
- Manage token limits and summaries
- Handle multi-turn conversations