This example demonstrates SochDB's Context Query Builder - a powerful feature for assembling LLM context under strict token budgets.
The Context Query Builder assembles the final prompt for an LLM by combining:
- System Message: Instructions and persona for the LLM
- User Query: The current user input
- Conversation History: Recent exchanges
- Retrieved Context: Semantically relevant information from vectorsearch
All of this is done under a token budget with priority-based truncation, ensuring the most important information fits within the LLM's context window.
- Token Budget Management: Automatically fits content within your specified token limit
- Priority-Based Trucation: Keeps the most important content (system + current query), then adds history and retrieval as space allows
- TOON Format Integration: Uses SochDB's
to_toon()for compact retrieval formatting - Flexible Truncation: Intelligently truncates conversation history from the middle, keeping recent and oldest context
- Chatbots: Keep conversation context relevant and within limits
- RAG Systems: Balance retrieved documents with conversation flow
- Long Conversations: Maintain coherence even after hundreds of turns
- Multi-Modal Agents: Coordinate different types of context (text, metadata, tool outputs)
pip install -r requirements.txt
python3 runner.pyThe script demonstrates:
- Building context with ample token budget → All content fits
- Building context with tight budget → Intelligent truncation
- Priority ordering → System and query always included first