This guide covers making a Letta MAS (Multi-Agent System) video production pipeline bulletproof with:
- Persistent patches - Custom Docker image with fixes baked in
- Auto-healing - Maintenance script that monitors and fixes issues
- Proactive protection - Cron jobs to prevent problems before they occur
Context window exceeded (current: 176539, threshold: 32000)
model_endpoint_type Input should be 'openai', 'anthropic'... input_value='claude-max-router'
- Agents accumulate messages over time
- When messages exceed threshold, Letta triggers summarization
- Summarization requires LLM call to custom endpoint
- Custom endpoint type not in Pydantic Literal validation
- Agent gets stuck in a loop, stops working
ARG LETTA_VERSION=latest
FROM letta/letta:${LETTA_VERSION}
LABEL maintainer="Letta MAS Admin"
LABEL description="Letta with claude-max-router endpoint support"
# Patch llm_config.py
RUN sed -i 's/"xai",/"xai",\n "claude-max-router",/' /app/letta/schemas/llm_config.py
# Patch model.py
RUN sed -i 's/"xai",/"xai",\n "claude-max-router",/' /app/letta/schemas/model.py
# Verify patches
RUN grep -q "claude-max-router" /app/letta/schemas/llm_config.py && \
echo "✓ llm_config.py patched" || echo "✗ PATCH FAILED"
RUN grep -q "claude-max-router" /app/letta/schemas/model.py && \
echo "✓ model.py patched" || echo "✗ PATCH FAILED"
ENTRYPOINT ["./letta/server/startup.sh"]# Build image
docker build -t letta-patched:bulletproof .
# Deploy (replace existing container)
docker stop letta-server
docker rm letta-server
docker run -d \
--name letta-server \
--restart unless-stopped \
-p 8283:8283 \
-e LETTA_PG_URI=postgresql+pg8000://letta:letta@host.docker.internal:5432/letta \
--add-host=host.docker.internal:host-gateway \
letta-patched:bulletproofAuto-monitors and heals agents before they hit critical levels.
- Monitors message count for all agents
- Auto-summarizes at 300+ messages
- Force-resets at 500+ messages (if summarization fails)
- Emergency reset at 600+ messages (no questions asked)
- Health checks for Letta, Router, ComfyUI
- Video generation staleness detection
- Comprehensive logging
MESSAGE_SUMMARIZE_THRESHOLD = 300 # Try to summarize
MESSAGE_RESET_THRESHOLD = 500 # Force reset if summarization failing
MESSAGE_CRITICAL_THRESHOLD = 600 # Emergency reset
VIDEO_STALE_MINUTES = 60 # Alert if no new video*/5 * * * * /usr/bin/python3 /home/straughter/letta_maintenance.py >> /tmp/letta_maintenance.log 2>&1[2026-01-15 16:31:18] [INFO] LETTA MAINTENANCE RUN STARTING
[2026-01-15 16:31:18] [INFO] Health: Letta=OK, Router=OK, ComfyUI=OK
[2026-01-15 16:31:18] [INFO] director has 37 messages
[2026-01-15 16:31:18] [INFO] writer has 171 messages
[2026-01-15 16:31:18] [WARN] cameraman at CRITICAL level (865 >= 600)
[2026-01-15 16:31:18] [SUCCESS] Message reset successful
If using a custom router (e.g., claude-max-router):
Letta calls /chat/completions without /v1 prefix:
app.post('/v1/chat/completions', handleChatCompletionsRequest);
app.post('/chat/completions', handleChatCompletionsRequest); // ADD THISForce router to use its own OAuth instead of passing through Letta's token:
npx tsx src/router/server.ts --disable-bearer-passthrough# Maintenance - every 5 minutes (monitors & auto-heals)
*/5 * * * * /usr/bin/python3 /home/straughter/letta_maintenance.py >> /tmp/letta_maintenance.log 2>&1
# Scheduler - every 15 minutes (processes production queue)
*/15 * * * * /home/straughter/letta_scheduler.py --mode queue >> /tmp/letta_cron.log 2>&1
# Daily batch - 2 AM (generates 5 new videos)
0 2 * * * /home/straughter/letta_scheduler.py --mode daily --count 5 >> /tmp/letta_cron.log 2>&1
# Prompt refill - every 30 minutes (keeps queue populated)
*/30 * * * * /usr/bin/python3 /home/straughter/prompt_generator.py --refill --count 20 >> /tmp/prompt_refill.log 2>&1# Check agent message counts
curl -sL "http://localhost:8283/v1/agents/" | \
jq -r '.[] | "\(.name): \(.message_ids | length) messages"'
# Check maintenance log
tail -50 /tmp/letta_maintenance.log
# Verify Docker patches
docker exec letta-server grep "claude-max-router" /app/letta/schemas/llm_config.py
# Check video count
ls ~/ComfyUI/output/video/*.mp4 | wc -l
# Check ComfyUI queue
curl -s http://localhost:8188/queue | jq '{running: .queue_running | length, pending: .queue_pending | length}'| Protection | Status |
|---|---|
| Persistent patches (Docker) | ✅ Survives rebuilds |
| Auto-summarization | ✅ Every 5 min check |
| Emergency reset | ✅ At 600+ messages |
| Health monitoring | ✅ Letta/Router/ComfyUI |
| Video staleness detection | ✅ 60 min threshold |
| Logging | ✅ /tmp/letta_maintenance.log |
| Docker restart policy | ✅ unless-stopped |
Protection Level: ~90%
The remaining 10% covers:
- Unknown edge cases
- Hardware failures
- Network issues
- OAuth token expiry (needs manual refresh)
| File | Purpose |
|---|---|
~/letta-patched/Dockerfile |
Patched Letta image |
~/letta_maintenance.py |
Auto-healing script |
~/letta_scheduler.py |
Production queue processor |
~/prompt_generator.py |
Auto-generates prompts |
/tmp/letta_maintenance.log |
Maintenance logs |
/tmp/letta_maintenance_state.json |
Persistent state |
Generated 2026-01-15