What You Get
- -Escalation task queue database
- -Priority and routing logic
- -Agent-to-human handoff API
- -Dashboard for human reviewers
- -Resolution tracking
Step by Step
1. Create the escalation database
Create PostgreSQL table: escalations (id uuid, source_agent text, issue_description text, priority text, context_data jsonb, status text, assigned_to uuid, created_at timestamptz, assigned_at timestamptz, resolved_at timestamptz, resolution_notes text, webhook_url text). Add indexes on status, priority, created_at.
2. Build the escalation API
Create REST endpoints: POST /api/escalations (create from agent), GET /api/escalations (list with filters), PATCH /api/escalations/:id/claim, PATCH /api/escalations/:id/resolve. All endpoints validate agent identity via API key.
3. Implement auto-assignment
On creation, auto-assign based on round-robin from active reviewers pool. For urgent priorities, also send Slack/email notification. Update status to 'assigned' and set assigned_at timestamp.
4. Build the real-time dashboard
Create a Next.js dashboard with: escalation list (sortable by priority, status, age), color-coded priority badges (red for urgent, yellow for high, blue for medium, gray for low), claim/resolve buttons, and WebSocket updates for new escalations.
5. Add resolution webhooks
When an escalation is resolved, POST the resolution (notes, resolution, timestamp) to the source agent's webhook_url. Include the original context_data for reference. Log delivery success/failure.
6. Build metrics and monitoring
Track: average time to first assignment, average resolution time by priority, escalation rate per agent, top 10 escalation reasons (parsed from issue_description via OpenAI categorization). Display on a metrics dashboard.
7. Add priority timeout escalation
Implement a cron job that checks for stale escalations: urgent >1 hour without assignment = notify on-call, high >4 hours = bump priority flag, medium >24 hours = notify manager. Update task appearance on dashboard.
Stack
Build This
Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to build this recipe.
Common Failure Modes
- !No human available to handle escalations
- !Escalation loop (agent re-escalates same issue)
- !Context too large to pass in escalation
- !Prioritization is ignored
Implementation Notes
Keep escalation context concise but complete. Set up on-call rotation for urgent escalations. Log every status change with timestamp.
Ship ai escalation queue in production with 4M Labs
4M Labs designs and ships applied AI systems -- connected to your tools, secured for your team, deployed with monitoring.
- Connected to your tools and data sources
- Secured for your team with proper access controls
- Deployed with monitoring and error handling
- Documented for handoff and future maintenance
Built by 4M Labs
This recipe is maintained by the 4M Labs engineering team. We build production AI systems for companies in Mexico and the US.
Frequently Asked Questions
- Can I use this recipe in production?
- Yes. Every recipe is production-tested with error handling, logging, and deployment guidance.
- Which LLM providers are supported?
- Recipes support OpenAI, Anthropic Claude, Google Gemini, and open-source models via a unified interface.
- How do I customize these recipes?
- Each recipe includes a configuration section. Override model selection, API keys, and parameters without changing core logic.