This guide covers the new LLM optimization features and LMStudio local model support added to QuietStories.
LMStudio is a desktop application that lets you run large language models locally on your computer. It provides an OpenAI-compatible API, making it easy to integrate with QuietStories.
-
Download LMStudio
- Visit: https://lmstudio.ai/
- Download and install for your OS
-
Load a Model
- Open LMStudio
- Browse models (recommended: Mistral 7B, Llama 2 7B, or similar)
- Download and load your chosen model
- Start the local server (usually on port 5101)
-
Configure QuietStories
Edit your .env file:
# Set provider to lmstudio
MODEL_PROVIDER=lmstudio
# LMStudio default endpoint (update port if different)
OPENAI_API_BASE=http://localhost:5101/v1
# Model name (can be anything for LMStudio)
MODEL_NAME=local-model
# API key not required for LMStudio
OPENAI_API_KEY=not-required- Start QuietStories
python -m uvicorn backend.main:app --reload- Verify Connection
Visit: http://localhost:8000/docs
Try the /optimization/stats endpoint to verify the system is working.
QuietStories now includes comprehensive optimization to reduce token usage and improve performance, especially important for local LLMs with limited context windows.
-
Context Caching
- Caches frequently used context strings
- Reduces repeated token usage
- LRU-style eviction when cache grows
-
Message Optimization
- Smart sliding window for conversation history
- Removes redundant messages
- Keeps system prompts intact
- Targets configurable token limits
-
Memory Summarization
- Filters memories by importance
- Keeps only recent/relevant memories
- Compresses long memory content
- Configurable per-entity limits
-
Automatic Memory Consolidation
- Runs every 10 turns automatically
- Removes low-importance memories
- Keeps memory size manageable
- Prevents context bloat over long sessions
User Input → Build Context → Optimize Messages → Call LLM → Process Response
↑ ↑
Memory Manager Reduce Tokens
(4000 default)
The optimizer:
- Estimates token usage
- If over limit, applies sliding window
- Prioritizes recent messages
- Preserves critical system context
QuietStories provides 4 presets optimized for different scenarios:
curl -X POST http://localhost:8000/optimization/presets/local_llmSettings:
- Max turn history: 5
- Max memories per entity: 5
- Max context tokens: 2000
- Caching: Enabled
Best for: Local models with 4K-8K context windows
curl -X POST http://localhost:8000/optimization/presets/cloud_llmSettings:
- Max turn history: 15
- Max memories per entity: 15
- Max context tokens: 8000
- Caching: Enabled
Best for: GPT-4, Claude, or other cloud models
curl -X POST http://localhost:8000/optimization/presets/minimalSettings:
- Max turn history: 3
- Max memories per entity: 3
- Max context tokens: 1000
- Caching: Enabled
Best for: Very small models or fastest performance
curl -X POST http://localhost:8000/optimization/presets/maximumSettings:
- Max turn history: 30
- Max memories per entity: 30
- Max context tokens: 16000
- Caching: Disabled
Best for: Best quality with large context models
You can fine-tune optimization settings via API:
curl -X POST http://localhost:8000/optimization/config \
-H "Content-Type: application/json" \
-d '{
"max_turn_history": 7,
"max_memories_per_entity": 8,
"max_context_tokens": 3000,
"enable_caching": true
}'curl http://localhost:8000/optimization/configGet current optimization configuration.
Response:
{
"max_turn_history": 10,
"max_memories_per_entity": 10,
"max_context_tokens": 4000,
"enable_caching": true
}Update optimization configuration.
Request Body:
{
"max_turn_history": 5,
"max_memories_per_entity": 5,
"max_context_tokens": 2000,
"enable_caching": true
}Get optimization statistics.
Response:
{
"cache_stats": {
"size": 15,
"max_size": 50,
"total_accesses": 142
},
"current_config": {
"max_turn_history": 10,
"max_memories_per_entity": 10,
"max_context_tokens": 4000,
"enable_caching": true
}
}Clear all optimization caches.
Response:
{
"status": "success",
"message": "Optimization caches cleared"
}List available optimization presets.
Apply a preset configuration.
Example:
curl -X POST http://localhost:8000/optimization/presets/local_llm-
Use the local_llm preset
curl -X POST http://localhost:8000/optimization/presets/local_llm
-
Choose appropriate models
- Best: Mistral 7B Instruct, Llama 2 7B Chat
- Avoid: Models larger than 13B (slow on most hardware)
- Context window: Prefer models with 4K+ context
-
Optimize LMStudio settings
- GPU acceleration: ON
- Context length: 4096 (matches optimization default)
- Temperature: 0.7 (good balance)
- Max tokens: 2048
-
Monitor token usage
- Check logs for "Optimized messages" counts
- If still slow, reduce
max_context_tokensfurther - Use
GET /optimization/statsto monitor cache hits
-
Use the cloud_llm preset
curl -X POST http://localhost:8000/optimization/presets/cloud_llm
-
For very long sessions (50+ turns)
- Memory consolidation runs automatically
- Check memory stats:
GET /sessions/{id}/memories - Manually consolidate if needed
-
Cost optimization
- Enable caching to reduce API calls
- Use
minimalpreset for development/testing - Switch to
cloud_llmfor production
-
Session Management
- Export important sessions:
GET /sessions/{id}/export(coming soon) - Clear old sessions periodically
- Memory consolidates automatically every 10 turns
- Export important sessions:
-
Debugging Performance
# Check token estimates in logs LOG_LEVEL=DEBUG python -m uvicorn backend.main:app --reload # Look for lines like: # [Optimizer] Current estimated tokens: 3500 # [Optimizer] Reduced from 20 to 15 messages (3500 -> 2200 tokens)
-
Cache Management
- Cache clears automatically (LRU eviction)
- Manually clear if behavior seems stale:
curl -X POST http://localhost:8000/optimization/cache/clear
Problem: "LMStudio API error: Connection refused"
Solutions:
- Verify LMStudio server is running (check LMStudio UI)
- Check port number in
.envmatches LMStudio - Try:
curl http://localhost:5101/v1/models - Restart LMStudio server
Problem: Story generation takes too long
Solutions:
- Apply minimal preset:
curl -X POST http://localhost:8000/optimization/presets/minimal
- Use smaller model (7B instead of 13B)
- Enable GPU acceleration in LMStudio
- Reduce
max_context_tokensfurther:curl -X POST http://localhost:8000/optimization/config \ -H "Content-Type: application/json" \ -d '{"max_context_tokens": 1500}'
Problem: "Out of memory" or very slow after many turns
Solutions:
- Memory consolidation should run automatically every 10 turns
- Check if it's working: Look for log message "Triggering memory consolidation"
- Restart session if issue persists
- Reduce memory limits:
curl -X POST http://localhost:8000/optimization/config \ -H "Content-Type: application/json" \ -d '{"max_memories_per_entity": 3}'
Problem: LLM seems to forget important details
Solutions:
- Increase context limits:
curl -X POST http://localhost:8000/optimization/presets/cloud_llm
- Check memory is being saved:
GET /sessions/{id}/memories - Verify model has sufficient context window (4K minimum)
- Disable aggressive optimization:
curl -X POST http://localhost:8000/optimization/config \ -H "Content-Type: application/json" \ -d '{"max_turn_history": 15, "max_memories_per_entity": 15}'
# Get statistics
curl http://localhost:8000/optimization/stats
# Response shows:
# - Cache hit/miss rates
# - Current configuration
# - Total cache accesses
# Example response:
{
"cache_stats": {
"size": 25, # Current cache size
"max_size": 50, # Cache capacity
"total_accesses": 342 # Total lookups
},
"current_config": { ... }
}# Get session memory stats
curl http://localhost:8000/sessions/{session_id}
# Look for:
# - turn: Current turn number
# - entities: Entity count
# - private_memory/public_memory: Memory sizesEnable debug logging to see optimization in action:
LOG_LEVEL=DEBUG python -m uvicorn backend.main:app --reloadLook for these log messages:
[Optimizer] Current estimated tokens: XXXX[Optimizer] Reduced from X to Y messages[Orchestrator] Triggering memory consolidation[Cache] Hit for key: ...(cache working)[LMStudio] Sending request to http://localhost:5101/v1
# 1. Configure for LMStudio
export MODEL_PROVIDER=lmstudio
export OPENAI_API_BASE=http://localhost:5101/v1
export MODEL_NAME=mistral-7b
# 2. Start backend
python -m uvicorn backend.main:app --reload
# 3. Apply optimization preset
curl -X POST http://localhost:8000/optimization/presets/local_llm
# 4. Create a session
curl -X POST http://localhost:8000/scenarios/generate \
-H "Content-Type: application/json" \
-d '{"description": "A detective mystery in a small town"}'
# (Save scenario ID from response)
curl -X POST http://localhost:8000/scenarios/{scenario_id}/compile
curl -X POST http://localhost:8000/sessions \
-H "Content-Type: application/json" \
-d '{"scenario_id": "{scenario_id}", "seed": 42}'
# (Save session ID from response)
# 5. Play the game
curl -X POST http://localhost:8000/sessions/{session_id}/turns \
-H "Content-Type: application/json" \
-d '{"action": "Investigate the crime scene"}'
# 6. Monitor optimization
curl http://localhost:8000/optimization/stats
# 7. Adjust if needed
curl -X POST http://localhost:8000/optimization/config \
-H "Content-Type: application/json" \
-d '{"max_context_tokens": 1500}'QuietStories now provides:
✅ LMStudio Support - Run stories with local models ✅ Smart Optimization - Automatic token reduction ✅ Memory Consolidation - Prevents context bloat ✅ Flexible Configuration - Presets + custom settings ✅ Performance Monitoring - Track optimization metrics
Recommended setup for most users:
MODEL_PROVIDER=lmstudiocurl -X POST http://localhost:8000/optimization/presets/local_llmEnjoy faster, more efficient story generation! 🚀