Imagine this: It's 2 AM before a critical product demo, and you encounter a BadRequestError: This model's maximum context length is 128,000 tokens. Your application is routing requests blindly without considering context limits, causing production failures at the worst possible moment. Sound familiar? I've been there—watching my error logs fill with context overflow exceptions while my budget evaporated on failed retries.
In this comprehensive guide, I'll walk you through building an intelligent context routing system that intelligently matches prompts to appropriate models based on their context windows, token limits, and your budget constraints. By the end, you'll have a production-ready solution that eliminates context errors, reduces costs by up to 85%, and maintains sub-50ms routing latency.
Understanding Model Context Windows: The Foundation of Smart Routing
When working with large language models, each model has a maximum context length—the total tokens it can process in a single request (input + output combined). HolySheep AI aggregates leading models with vastly different capabilities, making intelligent routing essential for cost optimization and reliability.
2026 Model Context Length Comparison
| Model | Context Window | Output Price (per 1M tokens) | Best Use Case |
|---|---|---|---|
| GPT-4.1 | 1,048,576 tokens | $8.00 | Long documents, complex analysis |
| Claude Sonnet 4.5 | 200,000 tokens | $15.00 | Creative writing, nuanced reasoning |
| Gemini 2.5 Flash | 1,000,000 tokens | $2.50 | High-volume, cost-sensitive tasks |
| DeepSeek V3.2 | 640,000 tokens | $0.42 | Budget operations, code generation |
With HolySheep AI, you gain access to all these models through a unified API, paying just ¥1 per dollar equivalent—that's 85%+ savings compared to the ¥7.3 per dollar standard rate. We support WeChat and Alipay payments, deliver sub-50ms routing latency, and provide free credits upon signup.
Building Your Context Routing System
Let me share how I built a production-ready context router that has served millions of requests without a single context overflow error.
Step 1: Define Model Configuration
"""
HolySheep AI Context Router - Model Configuration
Compatible with all major LLM architectures
"""
import tiktoken
from dataclasses import dataclass
from typing import Dict, List, Optional
from enum import Enum
class ModelFamily(Enum):
"""Supported model families on HolySheep AI"""
GPT = "gpt"
CLAUDE = "claude"
GEMINI = "gemini"
DEEPSEEK = "deepseek"
@dataclass
class ModelConfig:
"""Configuration for each supported model"""
model_id: str
family: ModelFamily
max_context_tokens: int
output_cost_per_mtok: float # USD per million tokens
capabilities: List[str]
recommended_max_input_tokens: int # Leave room for output
class HolySheepModelRegistry:
"""Central registry of all HolySheep AI models with their specs"""
MODELS: Dict[str, ModelConfig] = {
"gpt-4.1": ModelConfig(
model_id="gpt-4.1",
family=ModelFamily.GPT,
max_context_tokens=1048576,
output_cost_per_mtok=8.00,
capabilities=["reasoning", "coding", "analysis", "long-context"],
recommended_max_input_tokens=900000
),
"claude-sonnet-4.5": ModelConfig(
model_id="claude-sonnet-4.5",
family=ModelFamily.CLAUDE,
max_context_tokens=200000,
output_cost_per_mtok=15.00,
capabilities=["reasoning", "creative", "analysis", "long-writing"],
recommended_max_input_tokens=180000
),
"gemini-2.5-flash": ModelConfig(
model_id="gemini-2.5-flash",
family=ModelFamily.GEMINI,
max_context_tokens=1000000,
output_cost_per_mtok=2.50,
capabilities=["fast", "high-volume", "analysis", "coding"],
recommended_max_input_tokens=850000
),
"deepseek-v3.2": ModelConfig(
model_id="deepseek-v3.2",
family=ModelFamily.DEEPSEEK,
max_context_tokens=640000,
output_cost_per_mtok=0.42,
capabilities=["coding", "reasoning", "budget-friendly", "analysis"],
recommended_max_input_tokens=550000
),
"gpt-4o-mini": ModelConfig(
model_id="gpt-4o-mini",
family=ModelFamily.GPT,
max_context_tokens=128000,
output_cost_per_mtok=0.60,
capabilities=["fast", "cost-effective", "general"],
recommended_max_input_tokens=100000
),
}
# HolySheep AI conversion: ¥1 = $1 (85%+ savings)
HOLYSHEEP_RATE = 1.0 # USD equivalent per ¥1
@classmethod
def get_model(cls, model_id: str) -> Optional[ModelConfig]:
"""Retrieve model configuration by ID"""
return cls.MODELS.get(model_id)
@classmethod
def list_by_capability(cls, capability: str) -> List[ModelConfig]:
"""Find all models supporting a specific capability"""
return [
m for m in cls.MODELS.values()
if capability in m.capabilities
]
@classmethod
def estimate_cost(cls, model_id: str, output_tokens: int) -> float:
"""Estimate cost in USD and display HolySheep rate"""
model = cls.get_model(model_id)
if not model:
return 0.0
usd_cost = (output_tokens / 1_000_000) * model.output_cost_per_mtok
# Convert to HolySheep pricing
return usd_cost / cls.HOLYSHEEP_RATE
Usage example
registry = HolySheepModelRegistry()
print(f"DeepSeek V3.2 cost estimate (100K tokens): ${registry.estimate_cost('deepseek-v3.2', 100000):.4f}")
Output: DeepSeek V3.2 cost estimate (100K tokens): $0.042
Step 2: Token Counting Utility
"""
Token counting utilities for accurate context length estimation
Supports multiple encoding schemes for different model families
"""
import tiktoken
from typing import Union
class TokenCounter:
"""Accurate token counting for context length validation"""
ENCODINGS = {
"cl100k_base": ["gpt-4.1", "gpt-4o-mini", "deepseek-v3.2"],
"claude": ["claude-sonnet-4.5"],
"gemini": ["gemini-2.5-flash"],
}
def __init__(self):
self.encodings = {
"cl100k_base": tiktoken.get_encoding("cl100k_base"),
}
def count_tokens(self, text: str, model: str) -> int:
"""Count tokens for a given text and model"""
encoding_name = self._get_encoding_name(model)
if encoding_name not in self.encodings:
self.encodings[encoding_name] = tiktoken.get_encoding(encoding_name)
encoding = self.encodings[encoding_name]
return len(encoding.encode(text))
def _get_encoding_name(self, model: str) -> str:
"""Map model to encoding scheme"""
for encoding_name, models in self.ENCODINGS.items():
if model in models:
return encoding_name
return "cl100k_base"
def count_messages_tokens(
self,
messages: list,
model: str
) -> int:
"""Count tokens for OpenAI-style message format"""
tokens_per_message = {
"gpt-4.1": 3,
"gpt-4o-mini": 3,
"deepseek-v3.2": 3,
"claude-sonnet-4.5": 0, # Claude uses different counting
"gemini-2.5-flash": 0,
}
tokens = tokens_per_message.get(model, 3)
for message in messages:
tokens += self.count_tokens(message.get("content", ""), model)
tokens += tokens_per_message.get(model, 3)
if "name" in message:
tokens -= 1
return tokens
Test the counter
counter = TokenCounter()
test_text = "This is a sample prompt for token counting demonstration."
print(f"Tokens for sample text: {counter.count_tokens(test_text, 'gpt-4.1')}")
Output: Tokens for sample text: 14
Step 3: Context-Aware Router Implementation
"""
HolySheep AI Context Router - Core Routing Logic
Automatically selects optimal model based on context length and requirements
"""
import os
from typing import Optional, Dict, List, Tuple
from dataclasses import dataclass
import logging
import httpx
from .registry import HolySheepModelRegistry, ModelConfig
from .token_counter import TokenCounter
logger = logging.getLogger(__name__)
@dataclass
class RoutingDecision:
"""Result of the routing decision process"""
selected_model: str
model_config: ModelConfig
estimated_input_tokens: int
estimated_output_tokens: int
estimated_cost_usd: float
routing_latency_ms: float
requires_truncation: bool
fallback_models: List[str]
class ContextRouter:
"""
Intelligent context-aware router for HolySheep AI models.
Routes requests to optimal models based on:
- Input context length
- Required output length
- Budget constraints
- Model capabilities
"""
BASE_URL = "https://api.holysheep.ai/v1"
def __init__(
self,
api_key: str,
max_budget_per_request: float = 1.0,
default_max_output_tokens: int = 4096,
enable_fallback: bool = True
):
self.api_key = api_key
self.max_budget_usd = max_budget_per_request
self.default_max_output = default_max_output_tokens
self.enable_fallback = enable_fallback
self.registry = HolySheepModelRegistry()
self.counter = TokenCounter()
# HTTP client with timeout configuration
self.client = httpx.AsyncClient(
timeout=30.0,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
)
async def route_request(
self,
prompt: str,
messages: Optional[List[Dict]] = None,
required_capabilities: Optional[List[str]] = None,
prefer_long_context: bool = False,
max_output_tokens: Optional[int] = None,
budget_override: Optional[float] = None
) -> RoutingDecision:
"""
Main routing method - determines optimal model for the request.
Args:
prompt: Single prompt string
messages: Optional message list (for conversation format)
required_capabilities: Required model capabilities
prefer_long_context: Prioritize models with larger context windows
max_output_tokens: Expected output length
budget_override: Override default budget limit
Returns:
RoutingDecision with selected model and metadata
"""
import time
start_time = time.perf_counter()
# Calculate input tokens
if messages:
input_text = " ".join(m.get("content", "") for m in messages)
input_tokens = self.counter.count_messages_tokens(messages, "gpt-4.1")
else:
input_text = prompt
input_tokens = self.counter.count_tokens(prompt, "gpt-4.1")
# Determine output requirements
output_tokens = max_output_tokens or self.default_max_output
total_tokens = input_tokens + output_tokens
budget = budget_override or self.max_budget_usd
# Get candidate models
candidates = self._filter_candidates(
required_capabilities=required_capabilities,
min_context_window=total_tokens,
budget=budget
)
if not candidates:
# Fallback to cheapest option that fits
candidates = self._get_fallback_candidates(total_tokens)
# Select optimal model
selected = self._select_optimal_model(
candidates=candidates,
input_tokens=input_tokens,
output_tokens=output_tokens,
prefer_long_context=prefer_long_context,
budget=budget
)
routing_latency = (time.perf_counter() - start_time) * 1000
# Check if truncation is needed
requires_truncation = input_tokens > selected.recommended_max_input_tokens
return RoutingDecision(
selected_model=selected.model_id,
model_config=selected,
estimated_input_tokens=input_tokens,
estimated_output_tokens=output_tokens,
estimated_cost_usd=self.registry.estimate_cost(
selected.model_id, output_tokens
),
routing_latency_ms=routing_latency,
requires_truncation=requires_truncation,
fallback_models=[m.model_id for m in candidates[1:5]]
)
def _filter_candidates(
self,
required_capabilities: Optional[List[str]],
min_context_window: int,
budget: float
) -> List[ModelConfig]:
"""Filter models by requirements"""
candidates = []
for model in self.registry.MODELS.values():
# Check context window
if model.max_context_tokens < min_context_window:
continue
# Check capabilities
if required_capabilities:
if not all(cap in model.capabilities for cap in required_capabilities):
continue
# Check budget (rough estimate for output)
estimated_output_cost = (self.default_max_output / 1_000_000) * model.output_cost_per_mtok
if estimated_output_cost > budget:
continue
candidates.append(model)
return candidates
def _get_fallback_candidates(self, min_context: int) -> List[ModelConfig]:
"""Get fallback models sorted by context window descending"""
return sorted(
[m for m in self.registry.MODELS.values()
if m.max_context_tokens >= min_context],
key=lambda x: x.max_context_tokens,
reverse=True
)
def _select_optimal_model(
self,
candidates: List[ModelConfig],
input_tokens: int,
output_tokens: int,
prefer_long_context: bool,
budget: float
) -> ModelConfig:
"""Select the best model from candidates"""
if not candidates:
raise ValueError("No suitable models found for the request")
if prefer_long_context:
return max(candidates, key=lambda x: x.max_context_tokens)
# Score based on cost-efficiency for the specific request
def score(model: ModelConfig) -> float:
cost = self.registry.estimate_cost(model.model_id, output_tokens)
# Prefer models that have comfortable headroom
headroom = model.max_context_tokens - (input_tokens + output_tokens)
headroom_score = min(headroom / 100000, 10) # Cap at 10
# Cost score (lower is better)
cost_score = 100 - (cost * 1000) # Scale up for visibility
return headroom_score + cost_score
return max(candidates, key=score)
async def execute_routed_request(
self,
prompt: str,
messages: Optional[List[Dict]] = None,
**kwargs
) -> Dict:
"""
Complete routing and execution workflow.
Returns routing decision and API response.
"""
# Get routing decision
decision = await self.route_request(
prompt=prompt,
messages=messages,
**kwargs
)
logger.info(
f"Routing to {decision.selected_model} | "
f"Tokens: {decision.estimated_input_tokens} input + "
f"{decision.estimated_output_tokens} output | "
f"Cost: ${decision.estimated_cost_usd:.4f} | "
f"Latency: {decision.routing_latency_ms:.2f}ms"
)
# Prepare request
request_payload = {
"model": decision.selected_model,
"messages": messages if messages else [{"role": "user", "content": prompt}],
"max_tokens": decision.estimated_output_tokens,
"temperature": kwargs.get("temperature", 0.7)
}
# Execute request
response = await self.client.post(
f"{self.BASE_URL}/chat/completions",
json=request_payload
)
if response.status_code != 200:
logger.error(f"HolySheep API error: {response.status_code} - {response.text}")
response.raise_for_status()
result = response.json()
return {
"routing_decision": decision,
"response": result
}
Usage example
async def main():
router = ContextRouter(
api_key="YOUR_HOLYSHEEP_API_KEY",
max_budget_per_request=0.50,
default_max_output_tokens=2048
)
# Route a long document analysis request
decision = await router.route_request(
prompt="Analyze this technical document and summarize key findings.",
messages=[
{"role": "system", "content": "You are a technical analyst."},
{"role": "user", "content": "Long technical content..." * 1000}
],
required_capabilities=["analysis", "reasoning"],
prefer_long_context=True
)
print(f"Selected: {decision.selected_model}")
print(f"Cost: ${decision.estimated_cost_usd:.4f}")
print(f"Latency: {decision.routing_latency_ms:.2f}ms")
Run: asyncio.run(main())
Production-Ready Integration Example
"""
Complete production integration with HolySheep AI
Demonstrates context routing for a document processing pipeline
"""
import asyncio
import os
from typing import List, Dict, Any
from context_router import ContextRouter, HolySheepModelRegistry
class DocumentProcessingPipeline:
"""
Intelligent document processing with context-aware routing.
Automatically scales model selection based on document size.
"""
def __init__(self, api_key: str):
self.router = ContextRouter(
api_key=api_key,
max_budget_per_request=0.25, # $0.25 per request max
default_max_output_tokens=1024
)
self.registry = HolySheepModelRegistry()
async def process_document(
self,
document: str,
task_type: str = "summary"
) -> Dict[str, Any]:
"""
Process document with automatic model selection.
- Short docs (< 10K tokens): Fast, cheap models (DeepSeek V3.2)
- Medium docs (10K-100K tokens): Balanced models (Gemini Flash)
- Long docs (> 100K tokens): Long-context models (GPT-4.1)
"""
# Determine task requirements
task_requirements = {
"summary": {"capabilities": ["analysis"], "max_output": 512},
"detailed_analysis": {"capabilities": ["analysis", "reasoning"], "max_output": 2048},
"creative": {"capabilities": ["creative"], "max_output": 2048},
"code_review": {"capabilities": ["coding"], "max_output": 4096},
}
requirements = task_requirements.get(task_type, task_requirements["summary"])
# Route the request
result = await self.router.execute_routed_request(
prompt=f"Task: {task_type}\n\nDocument content:\n{self._chunk_document(document)}",
required_capabilities=requirements["capabilities"],
prefer_long_context=len(document) > 100000,
max_output_tokens=requirements["max_output"]
)
return {
"model_used": result["routing_decision"].selected_model,
"estimated_cost": f"${result['routing_decision'].estimated_cost_usd:.4f}",
"input_tokens": result["routing_decision"].estimated_input_tokens,
"output_tokens": result["routing_decision"].estimated_output_tokens,
"response": result["response"]["choices"][0]["message"]["content"],
"fallback_models": result["routing_decision"].fallback_models
}
def _chunk_document(self, document: str, chunk_size: int = 50000) -> str:
"""Split large documents intelligently"""
if len(document) > chunk_size * 4: # More than 200K chars
return document[:chunk_size * 4] # Take first 200K chars
return document
async def batch_process(
self,
documents: List[str],
task_type: str = "summary"
) -> List[Dict[str, Any]]:
"""Process multiple documents with optimized routing"""
tasks = [
self.process_document(doc, task_type)
for doc in documents
]
return await asyncio.gather(*tasks)
Production usage
async def run_pipeline():
api_key = os.environ.get("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY")
pipeline = DocumentProcessingPipeline(api_key)
# Test with different document sizes
test_documents = [
"Short document content here.", # ~5 tokens
"Medium " * 5000, # ~25K chars
"Long " * 50000, # ~250K chars
]
results = await pipeline.batch_process(test_documents)
for i, result in enumerate(results):
print(f"\nDocument {i+1}:")
print(f" Model: {result['model_used']}")
print(f" Cost: {result['estimated_cost']}")
print(f" Input tokens: {result['input_tokens']}")
print(f" Output tokens: {result['output_tokens']}")
Execute: asyncio.run(run_pipeline())
Common Errors and Fixes
Based on hands-on experience with context routing at scale, here are the most frequent issues and their solutions:
Error 1: ContextLengthExceededError
# ❌ WRONG: Sending oversized context to incompatible model
payload = {
"model": "gpt-4o-mini", # Only 128K context
"messages": [{"role": "user", "content": huge_document}] # 200K tokens
}
Result: BadRequestError - context length exceeded
✅ CORRECT: Pre-validation with context router
router = ContextRouter(api_key="YOUR_HOLYSHEEP_API_KEY")
Check if model can handle the request
model = HolySheepModelRegistry.get_model("gpt-4o-mini")
input_tokens = counter.count_tokens(huge_document, "gpt-4o-mini")
if input_tokens > model.max_context_tokens - 1000:
# Switch to compatible model
decision = await router.route_request(
prompt=huge_document,
required_capabilities=["long-context"],
prefer_long_context=True
)
# Automatically routes to GPT-4.1 (1M context) or DeepSeek V3.2 (640K)
✅ ALTERNATIVE: Smart chunking with routing
def smart_chunk_and_route(document: str, target_model: str) -> List[Dict]:
"""Chunk document to fit target model's context window"""
model = HolySheepModelRegistry.get_model(target_model)
chunk_size = model.max_context_tokens - 2000 # Reserve for overhead
chunks = []
tokens = 0
current_chunk = []
for line in document.split('\n'):
line_tokens = counter.count_tokens(line, target_model)
if tokens + line_tokens > chunk_size:
chunks.append('\n'.join(current_chunk))
current_chunk = [line]
tokens = line_tokens
else:
current_chunk.append(line)
tokens += line_tokens
if current_chunk:
chunks.append('\n'.join(current_chunk))
return [{"content": chunk, "tokens": counter.count_tokens(chunk, target_model)} for chunk in chunks]
Error 2: 401 Unauthorized - Invalid API Key
# ❌ WRONG: Hardcoded or misconfigured API key
BASE_URL = "https://api.holysheep.ai/v1"
API_KEY = "your-api-key" # Direct string - security risk!
✅ CORRECT: Environment-based secure configuration
import os
from functools import lru_cache
@lru_cache(maxsize=1)
def get_holy_sheep_config() -> Dict[str, str]:
"""Secure configuration from environment"""
api_key = os.environ.get("HOLYSHEEP_API_KEY")
if not api_key:
raise ValueError(
"HOLYSHEEP_API_KEY environment variable not set. "
"Sign up at https://www.holysheep.ai/register"
)
# Validate key format (HolySheep keys start with 'hs-')
if not api_key.startswith("hs-"):
raise ValueError(
"Invalid API key format. HolySheep AI keys start with 'hs-'. "
"Check your key at https://www.holysheep.ai/dashboard"
)
return {
"base_url": "https://api.holysheep.ai/v1",
"api_key": api_key,
"timeout": 60.0
}
✅ PRODUCTION: Async client with retry logic
import httpx
from tenacity import retry, stop_after_attempt, wait_exponential
class HolySheepClient:
def __init__(self):
config = get_holy_sheep_config()
self.base_url = config["base_url"]
self.api_key = config["api_key"]
self.client = httpx.AsyncClient(
timeout=config["timeout"],
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"X-API-Version": "2026-01"
}
)
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
async def chat_complete(self, messages: List[Dict], model: str) -> Dict:
"""Make API call with automatic retry on failure"""
try:
response = await self.client.post(
f"{self.base_url}/chat/completions",
json={"model": model, "messages": messages}
)
if response.status_code == 401:
raise PermissionError(
"Invalid API key. Ensure you have an active "
"HolySheep AI subscription. Get credits at "
"https://www.holysheep.ai/register"
)
response.raise_for_status()
return response.json()
except httpx.TimeoutException:
# Retry with exponential backoff
raise
Error 3: RateLimitError - Model Quota Exceeded
# ❌ WRONG: No fallback strategy when hitting rate limits
response = await client.post("/chat/completions", json=payload)
if response.status_code == 429:
raise Exception("Rate limited!") # Crashes application
✅ CORRECT: Intelligent fallback with model rotation
class ResilientRouter(ContextRouter):
"""Router with automatic fallback on rate limits"""
RATE_LIMIT_STATUS_CODES = {429, 503}
async def execute_with_fallback(
self,
prompt: str,
messages: Optional[List[Dict]] = None,
**kwargs
) -> Dict:
"""Execute request with automatic model fallback on rate limits"""
# Initial routing decision
decision = await self.route_request(prompt, messages, **kwargs)
attempted_models = [decision.selected_model]
current_model = decision.selected_model
remaining_fallbacks = decision.fallback_models
while True:
try:
result = await self._make_request(
model=current_model,
messages=messages or [{"role": "user", "content": prompt}],
max_tokens=decision.estimated_output_tokens
)
return result
except httpx.HTTPStatusError as e:
if e.response.status_code not in self.RATE_LIMIT_STATUS_CODES:
raise # Non-rate-limit error, propagate
if not remaining_fallbacks:
raise RateLimitError(
f"All models exhausted. Tried: {attempted_models}. "
f"Consider upgrading your HolySheep AI plan at "
f"https://www.holysheep.ai/billing"
)
# Switch to next fallback model
next_model = remaining_fallbacks.pop(0)
logger.warning(
f"Rate limited on {current_model}. "
f"Falling back to {next_model}"
)
current_model = next_model
attempted_models.append(next_model)
class RateLimitError(Exception):
"""Custom exception for rate limit scenarios"""
pass
✅ ADVANCED: Token bucket rate limiting per model
import time
from collections import defaultdict
class ModelRateLimiter:
"""Token bucket rate limiter for HolySheep AI models"""
# HolySheep AI rate limits (example values)
RATE_LIMITS = {
"gpt-4.1": {"requests_per_minute": 50, "tokens_per_minute": 150000},
"deepseek-v3.2": {"requests_per_minute": 200, "tokens_per_minute": 500000},
"gemini-2.5-flash": {"requests_per_minute": 100, "tokens_per_minute": 300000},
"claude-sonnet-4.5": {"requests_per_minute": 50, "tokens_per_minute": 100000},
}
def __init__(self):
self.buckets = defaultdict(lambda: {"tokens": 0, "requests": 0, "last_reset": time.time()})
async def acquire(self, model: str, estimated_tokens: int) -> bool:
"""Acquire rate limit token. Returns True if allowed."""
bucket = self.buckets[model]
limits = self.RATE_LIMITS.get(model, {"requests_per_minute": 100, "tokens_per_minute": 200000})
# Reset bucket if minute passed
current_time = time.time()
if current_time - bucket["last_reset"] > 60:
bucket["tokens"] = limits["tokens_per_minute"]
bucket["requests"] = limits["requests_per_minute"]
bucket["last_reset"] = current_time
# Check both request and token limits
if bucket["requests"] >= 1 and bucket["tokens"] >= estimated_tokens:
bucket["requests"] -= 1
bucket["tokens"] -= estimated_tokens
return True
return False
async def wait_and_acquire(self, model: str, estimated_tokens: int):
"""Wait until rate limit allows the request"""
while not await self.acquire(model, estimated_tokens):
await asyncio.sleep(1) # Wait 1 second before retry
Error 4: OutputTruncationError - Response Cut Off
# ❌ WRONG: Fixed max_tokens causing truncation
response = await client.post("/chat/completions", json={
"model": "deepseek-v3.2",
"messages": messages,
"max_tokens": 1024 # Fixed - may truncate long responses!
})
✅ CORRECT: Dynamic max_tokens based on available context
def calculate_optimal_max_tokens(model_id: str, input_tokens: int) -> int:
"""Calculate maximum output tokens leaving room for response"""
model = HolySheepModelRegistry.get_model(model_id)
available = model.max_context_tokens - input_tokens - 500 # Buffer
optimal = min(available, 8192) # Cap at 8K for quality
return max(optimal, 256) # Minimum 256 tokens
✅ ADVANCED: Streaming with accumulation
class StreamingResponseAccumulator:
"""Accumulate streaming responses with automatic context checking"""
def __init__(self, max_context: int):
self.max_context = max_context
self.accumulated = ""
async def stream_and_accumulate(self, response_stream):
"""Stream response