MATIH Platform is in active MVP development. Documentation reflects current implementation status.
12. AI Service
LLM Infrastructure
Context Management

Context Management

Production - Context window optimization and management

The Context Management service optimizes how conversation history, schema context, and system prompts are packed into the LLM context window.


12.6.3.1Context Window Configuration

max_context_tokens: int = 8000   # Maximum tokens for context
max_schema_tables: int = 50      # Maximum tables in schema context
max_sample_rows: int = 5         # Sample rows per table

Optimization Strategies

StrategyDescription
TruncationRemove oldest messages when exceeding token limit
SummarizationReplace old messages with LLM-generated summary
Relevance FilteringKeep only messages relevant to current query
Schema PruningInclude only tables/columns relevant to the question
# Optimize context
curl -X POST http://localhost:8000/api/v1/llm/context/optimize \
  -H "Content-Type: application/json" \
  -H "X-Tenant-ID: acme-corp" \
  -d '{
    "messages": [...],
    "max_tokens": 8000,
    "strategy": "relevance_filtering",
    "current_query": "What is total revenue?"
  }'