Intercept & Control AI Requests
Section titled “Intercept & Control AI Requests”The game-changer: Because Hoody AI requests flow through HTTP, you can intercept and modify everything using hoody-exec as a MITM (Man-In-The-Middle) proxy.
This isn’t about surveillance. It’s about complete control. When everything is HTTP (as explained in The HTTP Revolution), everything becomes observable, modifiable, and composable. AI requests are no different.
The breakthrough: Intercept, analyze, transform, cache, route, or enhance every AI interaction—all in just a few lines of JavaScript.
The simplicity: Deploy a MITM script once (see Deploying the MITM Script below), then just change the URL in your AI client — the base_url swap shown in the next section is the on-demand toggle, not the whole setup.
Without MITM: https://ai.hoody.icu/api/v1With MITM: https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1
Switch on-demand. No code changes. Complete control.How to Enable MITM (On-Demand)
Section titled “How to Enable MITM (On-Demand)”The beauty: You don’t need to change your code. Just change the URL in your AI client settings.
hoody-agent uses Hoody AI automatically — the AI gateway (base URL, key, model) is configured on the session/agent, not passed per request. To route the agent through your MITM proxy, point its configured AI base URL at hoody-exec:
- Normal Hoody AI:
https://ai.hoody.icu/api/v1 - With MITM enabled:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1
Then dispatch turns exactly as before — only the configured base URL changed:
curl -X POST "https://{projectId}-{containerId}-agent-1.{node}.containers.hoody.icu/api/v1/agent/sessions/{sessionId}/prompt:sync" \ -d '{ "text": "Build an app" }'Just change the configured base URL. Everything else stays the same.
Normal Hoody AI:
- Base URL:
https://ai.hoody.icu/api/v1 - API Key:
container-{containerName}
With MITM enabled:
- Base URL:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1 - API Key:
container-{containerName}(same)
Switch between these two URLs to enable/disable MITM features.
Normal Hoody AI:
- Base URL:
https://ai.hoody.icu/api/v1 - API Key:
container-{containerName} - Provider: Custom (OpenAI-compatible)
With MITM enabled:
- Base URL:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1 - API Key:
container-{containerName}(same) - Provider: Custom (OpenAI-compatible)
Toggle URL to switch modes instantly.
// Normal Hoody AIconst AI_URL = 'https://ai.hoody.icu/api/v1';
// With MITM enabledconst AI_URL = 'https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1';
// Rest of your code unchangedconst response = await fetch(`${AI_URL}/chat/completions`, { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ model, messages })});Use environment variable to switch modes without code changes.
On-demand activation: Want to test with MITM? Change the URL. Want to bypass MITM? Change back. No deployments. No config files. Just URL switching.
Why MITM AI Makes Sense
Section titled “Why MITM AI Makes Sense”The HTTP Advantage
Section titled “The HTTP Advantage”Traditional AI integrations are black boxes. You send a prompt, get a response. Everything in between is hidden.
With Hoody’s HTTP architecture:
- Every AI request is a visible HTTP call
- Every response flows through your infrastructure
- Every tool call is JSON you can inspect and modify
- Every agent decision is an HTTP endpoint you can intercept
This means: You can insert yourself (or your code) anywhere in the AI pipeline. Add logging. Request human approval. Transform prompts. Cache responses. Route to different models. Chain agents together. Replace tool calls. Inject context.
All through simple HTTP interception.
Key Capabilities
Section titled “Key Capabilities”Complete AI Observability:
- Log every prompt and response for debugging
- Track token usage per project automatically
- Analyze AI decision patterns
- Monitor for prompt injection attempts
- Build audit trails for compliance
Human-in-the-Loop at Scale:
- Intercept high-stakes decisions for human approval
- Pause AI execution for review before deployment
- Add confirmation steps for sensitive operations
- Let AI draft, humans decide
Cost Optimization:
- Compress prompts to reduce token usage (20-40% savings)
- Cache responses to eliminate duplicate calls (100% on cache hits)
- Route to cheaper models for simple tasks (40-70% savings)
- Auto-optimize based on complexity analysis
AI Enhancement:
- Add context from your knowledge base automatically
- Inject custom instructions per use case
- Transform responses to match your style
- Chain multiple AI calls intelligently
Tool Call Manipulation:
- Intercept and modify AI tool calls before execution
- Add safety checks to file operations
- Reroute dangerous commands to sandbox
- Replace file paths, command arguments, or entire operations
- Log all tool usage for audit trails
Agent Orchestration:
- Cascade AI requests across multiple agent instances
- Coordinate multi-agent workflows via HTTP
- Distribute tasks across agent swarms
- Build self-improving agent networks
The MITM Script Template
Section titled “The MITM Script Template”Here’s the basic pattern for creating an AI MITM proxy with hoody-exec:
// This catch-all route handles /api/v1/* (matching OpenAI API structure)// @mode worker// @log-level standard
// Handle all /api/v1/* endpoints (chat/completions, embeddings, images, etc.)const apiPath = metadata.parameters.path.join('/'); // e.g., "chat/completions"
const response = await fetch(`https://ai.hoody.icu/api/v1/${apiPath}`, { method: req.method, headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: req.method === 'POST' ? JSON.stringify(req.body) : undefined});
const data = await response.json();
// YOUR CUSTOM LOGIC HERE// - Modify prompts// - Add context// - Check cache// - Log for audit// - Request human approval// - Optimize model selection// - Intercept tool calls// - Trigger other agents
return res.json(data);File Structure for MITM Proxy
Section titled “File Structure for MITM Proxy”Option 1: Catch-All Route (Recommended - handles all AI endpoints)
/hoody/storage/hoody-exec/scripts/default/1/api/v1/[...path].jsThis handles:
POST /api/v1/chat/completionsPOST /api/v1/embeddingsGET /api/v1/models- Any other OpenAI-compatible endpoint
Option 2: Specific Endpoint (For targeted control)
/hoody/storage/hoody-exec/scripts/default/1/api/v1/chat/completions.jsThis only handles POST /api/v1/chat/completions
Accessing your MITM proxy:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1/chat/completionsHow to use:
- Deploy the script (see deployment section below)
- Change base URL in your AI client:
- Normal:
https://ai.hoody.icu/api/v1 - With MITM:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1
- Normal:
- That’s it. All requests now flow through your MITM proxy.
Switch on-demand: Toggle between URLs to enable/disable MITM. No code changes needed.
Deploying the MITM Script
Section titled “Deploying the MITM Script”Prerequisite — container identity token. The examples below use Bearer container-1. Hoody-minted containers are reachable under both a name-derived form (container-<name>) and a numbered form (container-<N>). Replace container-1 with the identifier that matches the container running the script; both forms are equivalent identity tokens, not copyable API keys.
Quick Deploy via hoody-files API
Section titled “Quick Deploy via hoody-files API”Create your MITM proxy script using the hoody-files API:
# Deploy the catch-all MITM proxy (handles all /api/v1/* endpoints)curl -X PUT "https://your-project-container-files-1.node-us.containers.hoody.icu/api/v1/files/hoody/storage/hoody-exec/scripts/default/1/api/v1/%5B...path%5D.js" \ -H "Content-Type: application/octet-stream" \ --data-binary @- << 'EOF'// File: scripts/default/1/api/v1/[...path].js// Catch-all MITM proxy for all OpenAI-compatible endpoints// @mode worker// @log-level standard
// Handle all /api/v1/* endpointsconst apiPath = metadata.parameters.path.join('/');
const response = await fetch(`https://ai.hoody.icu/api/v1/${apiPath}`, { method: req.method, headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: req.method === 'POST' ? JSON.stringify(req.body) : undefined});
const data = await response.json();
// YOUR MITM LOGIC HERE// Example: Log all requestsconsole.log(`AI Request: ${req.method} /api/v1/${apiPath}`);console.log(`Model: ${req.body?.model || 'N/A'}`);console.log(`Tokens: ${data.usage?.total_tokens || 'N/A'}`);
return res.json(data);EOFThat’s it. Your MITM proxy is now live at:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1/chat/completionsQuick Deploy via the hoody-exec scripts API (recommended)
Section titled “Quick Deploy via the hoody-exec scripts API (recommended)”# Create the script using hoody-exec's script management APIcurl -X POST "https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1/exec/scripts/write" \ -H "Content-Type: application/json" \ -d @- << 'EOF'{ "path": "default/1/api/v1/[...path].js", "content": "// @mode worker\n// @log-level standard\n\nconst apiPath = metadata.parameters.path.join('/');\n\nconst response = await fetch(`https://ai.hoody.icu/api/v1/${apiPath}`, {\n method: req.method,\n headers: {\n 'Authorization': 'Bearer container-1',\n 'Content-Type': 'application/json'\n },\n body: req.method === 'POST' ? JSON.stringify(req.body) : undefined\n});\n\nconst data = await response.json();\nconsole.log(`AI Request: ${req.method} /api/v1/${apiPath}`);\n\nreturn res.json(data);"}EOFVerify Deployment
Section titled “Verify Deployment”# Test your MITM proxycurl -X POST "https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1/chat/completions" \ -H "Authorization: Bearer container-1" \ -H "Content-Type: application/json" \ -d '{ "model": "anthropic/claude-haiku-4.0", "messages": [{"role": "user", "content": "Hello!"}] }'If you see the response and logs, your MITM is working!
Real-World Use Cases
Section titled “Real-World Use Cases”1. Human-in-the-Loop Gating
Section titled “1. Human-in-the-Loop Gating”Stop AI from executing high-stakes operations without human approval:
// @mode worker// @log-level standard
const lastMessage = req.body.messages[req.body.messages.length - 1].content;
// Detect high-stakes operationsconst isHighStakes = /deploy|delete|drop|production|payment|transfer/.test( lastMessage.toLowerCase());
if (isHighStakes) { // Store request for approval workflow const requestId = crypto.randomUUID();
// Send notification to human via hoody-notifications await fetch('https://your-project-container-n-1.node-us.containers.hoody.icu/api/v1/notifications/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ display: ':1', summary: 'AI Needs Approval', body: `High-stakes operation detected:\n${lastMessage}\nRequest ID: ${requestId}`, urgency: 'critical' }) });
if (!shared.pendingApprovals) shared.pendingApprovals = new Map(); shared.pendingApprovals.set(requestId, req.body);
return res.json({ status: 'pending_approval', requestId, message: 'High-stakes operation detected. Awaiting human approval.', estimatedWait: '2-10 minutes' });}
// Normal flow for safe operationsconst response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
return res.json(await response.json());The transformation: AI agents can work autonomously 95% of the time. Critical decisions pause for your review. You become the approval layer, not the execution layer.
The workflow at scale: dozens of agents working across your containers. Your job? Answer decision requests. “Deploy to production?” “Delete this database?” You confirm. They execute.
2. Tool Call Interception & Tampering
Section titled “2. Tool Call Interception & Tampering”The killer feature: AI agents use tool calls to interact with files, execute commands, etc. You can intercept and modify these tool calls before they execute.
Redirect file operations to sandbox:
// @mode worker
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();
// Intercept tool calls before they executeif (data.choices[0].message.tool_calls) { data.choices[0].message.tool_calls = data.choices[0].message.tool_calls.map(call => { // Redirect dangerous file operations to sandbox if (call.function.name === 'write_file') { const args = JSON.parse(call.function.arguments);
// Force all writes into /sandbox/ directory if (!args.path.startsWith('/sandbox/')) { args.path = '/sandbox' + args.path; call.function.arguments = JSON.stringify(args); } }
// Add safety checks to delete operations if (call.function.name === 'delete_file') { const args = JSON.parse(call.function.arguments);
// Prevent deletion of critical files if (args.path.match(/config|production|\.env|package\.json/)) { // Replace with confirmation tool call.function.name = 'confirm_delete'; call.function.arguments = JSON.stringify({ ...args, warning: 'Critical file deletion requires confirmation' }); } }
// Intercept command execution if (call.function.name === 'execute_command') { const args = JSON.parse(call.function.arguments);
// Block or modify dangerous commands if (args.command.match(/rm -rf|sudo|chmod 777/)) { call.function.name = 'blocked_command'; call.function.arguments = JSON.stringify({ original: args.command, reason: 'Dangerous command intercepted' }); } }
return call; });}
return res.json(data);Use cases:
- AI can code freely, but file writes are automatically sandboxed
- Dangerous operations require explicit confirmation
- Critical files are protected from accidental deletion
- Command injection is prevented
The power: AI operates with full freedom, but you’ve added guardrails that prevent catastrophic mistakes. All done in ~50 lines of code.
3. Agent Cascade Orchestration
Section titled “3. Agent Cascade Orchestration”The breakthrough: Because every service is HTTP, you can trigger cascades of agents from your MITM proxy.
Multi-agent coordination:
// @mode worker
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();const aiResponse = data.choices[0].message.content;
// Detect when AI wants to delegate workif (aiResponse.includes('DELEGATE:')) { const taskMatch = aiResponse.match(/DELEGATE: (.+)/); const delegatedTask = taskMatch[1];
// Cascade to another hoody-agent via HTTP (one-shot headless run) const agentResponse = await fetch( 'https://project-container-agent-2.node-us.containers.hoody.icu/api/v1/agent/headless/runs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: delegatedTask, model: 'anthropic/claude-sonnet-4.5' }) } );
const agentData = await agentResponse.json();
// Modify response to indicate delegation data.choices[0].message.content = `Task delegated to Agent-2: ${delegatedTask}\n` + `Job ID: ${agentData.job_id}\n` + `Status: In progress...`;}
// Detect when AI needs specialized capabilitiesif (aiResponse.includes('ANALYZE_CODE:')) { // Trigger code analysis agent await fetch('https://project-analyzer-agent-1.node-us.containers.hoody.icu/api/v1/agent/headless/runs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: 'Analyze codebase for security issues' }) });}
return res.json(data);What this enables:
- Agent swarms - One agent spawns/coordinates 10 others
- Specialized agents - Route tasks to expert agents (code, security, design)
- Parallel execution - Distribute work across multiple agents simultaneously
- Self-organizing systems - Agents discover and coordinate with each other
Real scenario: You ask Agent A to “build a complete SaaS app”. Agent A analyzes, then cascades to:
- Agent B (frontend specialist)
- Agent C (backend specialist)
- Agent D (database specialist)
- Agent E (security auditor)
All coordinating via HTTP. All reporting back. All orchestrated from one MITM script.
4. Request Stalling with Notifications
Section titled “4. Request Stalling with Notifications”Powerful pattern: Pause AI execution until human reviews and approves.
// @mode worker// @log-level standard
// Initialize shared stateif (!shared.pendingRequests) { shared.pendingRequests = new Map();}
if (req.body.urgent === true) { const requestId = crypto.randomUUID();
// Store request shared.pendingRequests.set(requestId, { request: req.body, timestamp: Date.now(), status: 'pending' });
// Notify human via hoody-notifications await fetch('https://your-project-container-n-1.node-us.containers.hoody.icu/api/v1/notifications/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ display: ':1', summary: 'AI Awaiting Approval', body: `${req.body.messages[req.body.messages.length - 1].content}\nApprove: /api/approve?id=${requestId} — Reject: /api/reject?id=${requestId}`, urgency: 'critical' }) });
// Poll for approval (or use webhook callback) for (let i = 0; i < 300; i++) { // 5 minutes max await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second
const request = shared.pendingRequests.get(requestId); if (request.status === 'approved') { break; } else if (request.status === 'rejected') { return res.status(403).json({ error: 'Request rejected by human' }); } }
// Timeout if no response if (shared.pendingRequests.get(requestId).status === 'pending') { return res.status(408).json({ error: 'Approval timeout' }); }}
// Proceed with AI requestconst response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
return res.json(await response.json());The workflow:
- AI agent wants to deploy to production
- MITM detects high-stakes operation
- Notification sent to the container’s display
- AI request pauses (stalls)
- You review and approve/reject
- AI continues or stops based on your decision
This is human-in-the-loop at scale. Dozens of agents working; you approve only the handful of decisions that matter.
5. Tool Call Tampering for Safety
Section titled “5. Tool Call Tampering for Safety”The most powerful pattern: Completely replace what AI tools do.
Example: Reroute all file writes to version control:
// @mode worker
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();
// Intercept and replace tool callsif (data.choices[0].message.tool_calls) { for (const call of data.choices[0].message.tool_calls) { // Replace write_file with version-controlled write if (call.function.name === 'write_file') { const args = JSON.parse(call.function.arguments);
// Create git commit for this change await fetch('https://your-project-container-terminal-1.node-us.containers.hoody.icu/api/v1/terminal/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: `git add ${args.path} && git commit -m "AI: ${args.description || 'auto-save'}"` }) });
// Modify the tool call to include git metadata call.function.arguments = JSON.stringify({ ...args, git_tracked: true, commit_message: `AI: ${args.description || 'auto-save'}` }); }
// Replace read_file to inject AI-generated documentation if (call.function.name === 'read_file') { const args = JSON.parse(call.function.arguments);
// Read actual file via hoody-files const fileResponse = await fetch( `https://your-project-container-files-1.node-us.containers.hoody.icu/api/v1/files${args.path}`, { method: 'GET' } ); const fileContent = await fileResponse.text();
// Ask AI to add inline documentation const docResponse = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'anthropic/claude-haiku-4.0', messages: [{ role: 'user', content: `Add inline documentation to this code:\n${fileContent}` }] }) });
const documented = await docResponse.json();
// Return documented version instead call.function.arguments = JSON.stringify({ ...args, enhanced: true, content: documented.choices[0].message.content }); } }}
return res.json(data);What you’ve done:
- Every file write is now automatically version controlled
- Every file read is enhanced with AI-generated documentation
- AI sees improved code context automatically
- You have full audit trail of all changes
The magic: AI doesn’t know you’re intercepting. It just works with better data and safer operations.
6. Cost Optimization: Intelligent Model Routing
Section titled “6. Cost Optimization: Intelligent Model Routing”Save 40-70% by routing to cheaper models automatically:
// @mode worker
const lastMessage = req.body.messages[req.body.messages.length - 1].content;
// Analyze complexityconst wordCount = lastMessage.split(/\s+/).length;const hasCode = /```|function|class|import|async|await/.test(lastMessage);const hasMultiStep = /step|then|after|finally|workflow/.test(lastMessage);const isDeployment = /deploy|production|release/.test(lastMessage);
// Intelligent model selectionlet selectedModel = req.body.model;
if (isDeployment) { // Critical operations get best model selectedModel = 'anthropic/claude-opus-4.1';} else if (wordCount < 50 && !hasCode && !hasMultiStep) { // Simple question → cheapest model selectedModel = 'anthropic/claude-haiku-4.0';} else if (hasCode || hasMultiStep) { // Complex reasoning → balanced model selectedModel = 'anthropic/claude-sonnet-4.5'; // $3.00/M tokens} else { // General tasks → fast model selectedModel = 'openai/gpt-4o'; // $2.50/M tokens}
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ ...req.body, model: selectedModel // Override with optimal model })});
return res.json(await response.json());Savings: 40-70% by automatically using cheaper models when appropriate. AI quality doesn’t suffer—simple tasks don’t need expensive models.
7. Context Injection from Knowledge Base
Section titled “7. Context Injection from Knowledge Base”Auto-enhance AI with your company knowledge:
// @mode worker
// Modules auto-installed on first requireconst { createClient } = require('@supabase/supabase-js');
const supabase = createClient( process.env.SUPABASE_URL, process.env.SUPABASE_KEY);
const userPrompt = req.body.messages[req.body.messages.length - 1].content;
// Semantic search in your knowledge baseconst { data: context } = await supabase .from('documentation') .select('content, source, relevance') .textSearch('content', userPrompt) .order('relevance', { ascending: false }) .limit(3);
// Inject context into system promptconst enhancedMessages = [ { role: 'system', content: `You have access to our internal knowledge base. Relevant context for this request:\n\n${ context.map(c => `**${c.source}**:\n${c.content}`).join('\n\n') }\n\nUse this context to provide accurate, company-specific answers.` }, ...req.body.messages];
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ ...req.body, messages: enhancedMessages })});
return res.json(await response.json());The result: AI always has access to your latest documentation, internal wikis, company policies, and codebase context—automatically. No manual RAG setup. Just HTTP interception.
8. Response Caching for Cost Reduction
Section titled “8. Response Caching for Cost Reduction”Eliminate duplicate AI calls:
// @mode worker
const { createClient } = require('@supabase/supabase-js');const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY);
// Create cache key from requestconst cacheKey = JSON.stringify({ model: req.body.model, messages: req.body.messages});
// Check cache (using hoody-sqlite for persistence)const { data: cached } = await supabase .from('ai_cache') .select('response') .eq('cache_key', cacheKey) .single();
if (cached) { return res.json({ ...cached.response, cached: true, savings: '100% (from cache)' });}
// Call AIconst response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();
// Store in cacheawait supabase .from('ai_cache') .insert({ cache_key: cacheKey, response: data, created_at: new Date().toISOString() });
return res.json(data);Savings: 100% cost reduction on cache hits. Perfect for:
- Repeated questions (documentation, support)
- Code reviews (similar code patterns)
- Content generation (similar prompts)
9. Prompt Compression
Section titled “9. Prompt Compression”Reduce token usage 20-40%:
// @mode worker
function compressPrompt(text) { return text .replace(/\s+/g, ' ') // Normalize whitespace .replace(/\b(the|a|an)\b/gi, '') // Remove articles .replace(/\b(please|kindly|could you)\b/gi, '') // Remove pleasantries .trim();}
// Compress verbose promptsconst compressedMessages = req.body.messages.map(msg => ({ ...msg, content: compressPrompt(msg.content)}));
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ ...req.body, messages: compressedMessages })});
return res.json(await response.json());Savings: 20-40% on verbose inputs without losing semantic meaning.
Advanced Patterns
Section titled “Advanced Patterns”Complete AI Request Logging
Section titled “Complete AI Request Logging”Build audit trails automatically:
// @mode worker
const { createClient } = require('@supabase/supabase-js');const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY);
const requestId = crypto.randomUUID();const startTime = Date.now();
// Log requestawait supabase.from('ai_logs').insert({ request_id: requestId, container: metadata.executionId, model: req.body.model, prompt: req.body.messages[req.body.messages.length - 1].content, timestamp: new Date().toISOString()});
// Make AI requestconst response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();const duration = Date.now() - startTime;
// Log responseawait supabase.from('ai_logs').update({ response: data.choices[0].message.content, tokens_used: data.usage?.total_tokens, duration_ms: duration, cost: (data.usage?.total_tokens || 0) * 0.000003 // Example cost calculation}).eq('request_id', requestId);
return res.json(data);You now have:
- Complete audit trail of all AI interactions
- Token usage per container/project
- Cost tracking automatically
- Performance metrics
- Compliance documentation
Multi-Provider Failover
Section titled “Multi-Provider Failover”Use Hoody AI, fallback to your own keys:
// @mode worker
// Try Hoody AI firstlet response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
// Fallback to direct OpenAI if Hoody AI failsif (!response.ok && response.status >= 500) { response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.OPENAI_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify(req.body) });}
// Fallback to Anthropic if OpenAI failsif (!response.ok && response.status >= 500) { response = await fetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'x-api-key': process.env.ANTHROPIC_KEY, 'anthropic-version': '2023-06-01', 'Content-Type': 'application/json' }, body: JSON.stringify({ model: req.body.model.replace('anthropic/', ''), max_tokens: req.body.max_tokens || 1024, messages: req.body.messages }) });}
return res.json(await response.json());Resilience: Automatic failover across providers. Zero downtime.
Response Enhancement
Section titled “Response Enhancement”AI generates code, you automatically add explanations:
// @mode worker
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();let content = data.choices[0].message.content;
// Detect code blocksif (content.includes('```')) { // Ask another AI to explain the code const explanation = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'anthropic/claude-haiku-4.0', // Cheap model for simple task messages: [{ role: 'user', content: `Explain this code in simple terms:\n${content}` }] }) });
const explainData = await explanation.json();
// Append explanation content += '\n\n**How this works:**\n' + explainData.choices[0].message.content; data.choices[0].message.content = content;}
return data;10. Real-Time Alerts on Your Displays
Section titled “10. Real-Time Alerts on Your Displays”Cool integration: Get instant desktop notifications about AI activity on your container display using hoody-notifications:
// @mode worker
const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();
// Detect significant eventsconst aiResponse = data.choices[0].message.content;const isCodeGeneration = aiResponse.includes('```') && aiResponse.length > 500;const isError = data.error || data.choices[0].finish_reason === 'error';
if (isCodeGeneration) { // Notify about large code generation await fetch('https://your-project-container-n-1.node-us.containers.hoody.icu/api/v1/notifications/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ display: ':1', summary: 'AI Generated Code', body: `AI just wrote ${aiResponse.length} characters of code`, urgency: 'normal', icon: 'code' }) });}
if (isError) { // Alert about AI errors await fetch('https://your-project-container-n-1.node-us.containers.hoody.icu/api/v1/notifications/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ display: ':1', summary: 'AI Request Failed', body: data.error?.message || 'Unknown error', urgency: 'critical', icon: 'warning' }) });}
// Track token usage and alert on thresholdif (data.usage?.total_tokens > 50000) { await fetch('https://your-project-container-n-1.node-us.containers.hoody.icu/api/v1/notifications/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ display: ':1', summary: 'High Token Usage', body: `Request used ${data.usage.total_tokens} tokens`, urgency: 'normal' }) });}
return res.json(data);Real-world scenarios:
- Get notified when AI generates large amounts of code
- Alert on AI errors or rate limits
- Track high token usage in real-time
- Monitor AI agent activity from any open display session
- Know immediately when critical operations complete
The power: Your entire AI infrastructure raises desktop alerts on the container displays you already have open. Stay informed without actively monitoring.
11. Complete Prompt History with SQLite
Section titled “11. Complete Prompt History with SQLite”Store all AI interactions in a database using Bun’s built-in SQLite and /hoody/databases/ for automatic safety:
// @mode worker
// Use Bun's built-in sqlite3 (no npm install needed with Bun)const { Database } = require('bun:sqlite');
// Database in /hoody/databases/ = automatic concurrent-write safety// Thanks to SQLite Drive FUSE mount - prevents corruption from concurrent writesconst db = new Database('/hoody/databases/ai-history.db');
// Create table on first runif (!shared.initialized) { db.run(` CREATE TABLE IF NOT EXISTS prompts ( id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT NOT NULL, container TEXT, model TEXT, prompt TEXT, response TEXT, tokens INTEGER, cost REAL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) `);
db.run('CREATE INDEX IF NOT EXISTS idx_timestamp ON prompts(timestamp DESC)'); db.run('CREATE INDEX IF NOT EXISTS idx_model ON prompts(model)'); db.run('CREATE INDEX IF NOT EXISTS idx_container ON prompts(container)');
shared.initialized = true;}
// Make AI requestconst response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer container-1', 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
const data = await response.json();
// Store complete interaction in database (concurrent-write safe)db.run(` INSERT INTO prompts (timestamp, container, model, prompt, response, tokens, cost) VALUES (?, ?, ?, ?, ?, ?, ?)`, [ new Date().toISOString(), metadata.executionId || 'default', req.body.model, req.body.messages[req.body.messages.length - 1].content, data.choices[0].message.content, data.usage?.total_tokens || 0, (data.usage?.total_tokens || 0) * 0.000003 // Example cost calc]);
return res.json(data);Why /hoody/databases/ is critical here:
- Multiple containers can log simultaneously without corruption
- AI agents making parallel requests all write safely
- FUSE mount coordinates writes automatically
- Zero locking errors even under heavy concurrent load
See: SQLite Drive for full details on concurrent-write safety.
Query your AI history:
# View recent promptsbun -e 'const db = require("bun:sqlite").Database("/hoody/databases/ai-history.db"); console.log(db.query("SELECT model, prompt, tokens FROM prompts ORDER BY created_at DESC LIMIT 10").all())'
# Total tokens used per modelbun -e 'const db = require("bun:sqlite").Database("/hoody/databases/ai-history.db"); console.log(db.query("SELECT model, SUM(tokens) as total FROM prompts GROUP BY model").all())'
# Most expensive queriesbun -e 'const db = require("bun:sqlite").Database("/hoody/databases/ai-history.db"); console.log(db.query("SELECT prompt, cost FROM prompts ORDER BY cost DESC LIMIT 5").all())'
# Usage by containerbun -e 'const db = require("bun:sqlite").Database("/hoody/databases/ai-history.db"); console.log(db.query("SELECT container, COUNT(*) as requests, SUM(tokens) as tokens FROM prompts GROUP BY container").all())'Use cases:
- Audit trails - Complete record of all AI interactions
- Cost analytics - Track spending by model, container, time period
- Pattern analysis - Identify frequently repeated prompts for caching
- Debugging - Review exact prompts/responses when issues occur
- Compliance - Maintain detailed logs for regulatory requirements
- RAG data - Use your prompt history as training data for fine-tuning
Using Any AI Gateway
Section titled “Using Any AI Gateway”Critical freedom: It’s YOUR infrastructure. Route to whoever you want.
// @mode worker
// Route to OpenAI directlyconst response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.OPENAI_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify(req.body)});
return res.json(await response.json());
// Or use any other provider:// - Anthropic directly// - Together AI// - Your own self-hosted models (Ollama, LM Studio)// - Multiple providers with custom routing logicYou decide:
- Which provider for which tasks
- When to use Hoody AI vs your own keys
- How to distribute load
- Where costs should go
The only rule: It’s HTTP. Route however you want.
Use Cases
Section titled “Use Cases”1. Safe Vibe Coding
Section titled “1. Safe Vibe Coding”Let AI generate entire applications, but with guardrails:
// Sandbox all AI file operationsif (call.function.name === 'write_file') { args.path = '/sandbox' + args.path;}
// Block dangerous commandsif (call.function.name === 'execute_command') { if (args.command.match(/rm -rf|sudo|chmod 777/)) { return blocked(); }}2. Multi-Tenant AI SaaS
Section titled “2. Multi-Tenant AI SaaS”Each customer gets MITM’d AI access:
const customer = getCustomerFromContainer(metadata.parameters.tenant);
// Customer-specific quota enforcementif (customer.tokensUsedToday > customer.quota) { return res.status(429).json({ error: 'Quota exceeded' });}
// Customer-specific model restrictionsif (!customer.allowedModels.includes(req.body.model)) { req.body.model = customer.defaultModel;}3. Development → Production Pipeline
Section titled “3. Development → Production Pipeline”Different MITM rules per environment:
// Development: Log everything, use cheap models// (pass ?env=dev / ?env=prod — surfaced via metadata.parameters)if (metadata.parameters.env === 'dev') { console.log('Request:', req.body); req.body.model = 'anthropic/claude-haiku-4.0';}
// Production: Route to best model, alert on errorsif (metadata.parameters.env === 'prod') { req.body.model = 'anthropic/claude-opus-4.1'; // monitorForErrors() implementation}Best Practices
Section titled “Best Practices”Layer Your MITM Logic
Section titled “Layer Your MITM Logic”Don’t try to do everything in one script. Chain multiple MITM proxies:
App → MITM Layer 1 (logging) → MITM Layer 2 (caching) → MITM Layer 3 (model routing) → Hoody AIEach layer does one thing well. Composable intelligence.
Use hoody-sqlite for State
Section titled “Use hoody-sqlite for State”Store caches, approval requests, logs in hoody-sqlite:
// Persistent cache across restartsawait fetch('https://your-project-container-sqlite-1.node-us.containers.hoody.icu/api/v1/sqlite/kv/batch/set?db=ai-cache.db', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ items: [{ key: cacheKey, value: aiResponse }] })});Monitor MITM Performance
Section titled “Monitor MITM Performance”Your MITM proxy adds latency. Measure it:
const start = Date.now();const response = await fetch('https://ai.hoody.icu/api/v1/chat/completions', { /* ... */ });const latency = Date.now() - start;
if (latency > 1000) { console.warn('MITM proxy slow:', latency, 'ms');}Start Simple, Add Complexity
Section titled “Start Simple, Add Complexity”Day 1: Just log requests
Day 2: Add caching
Day 3: Add model routing
Day 4: Add human approval for high-stakes
Day 5: Add agent cascade
Build incrementally. Each layer adds value.
Troubleshooting
Section titled “Troubleshooting”MITM Proxy Not Being Called
Section titled “MITM Proxy Not Being Called”Problem: Requests bypass your proxy
Solution: Ensure apps point to your hoody-exec endpoint:
https://your-project-container-exec-1.node-us.containers.hoody.icu/api/v1NOT: https://ai.hoody.icu/api/v1Tool Calls Not Being Intercepted
Section titled “Tool Calls Not Being Intercepted”Problem: Modifications to tool_calls don’t take effect
Solution: Return the modified data BEFORE the tool executes:
// Correct: Modify in response, before tool executiondata.choices[0].message.tool_calls = modified;return res.json(data);
// Wrong: Trying to modify after executionResponse Caching Issues
Section titled “Response Caching Issues”Problem: Cached responses are stale
Solution: Add cache invalidation:
const cacheKey = `${model}:${JSON.stringify(messages)}:${Math.floor(Date.now() / 3600000)}`;// Key changes every hour, auto-invalidatesHuman Approval Timeout
Section titled “Human Approval Timeout”Problem: Stalled requests timeout before human responds
Solution: Increase timeout or implement webhook callback:
// Webhook approach (better):// 1. Store request// 2. Send notification with callback URL// 3. Return immediately with "pending" status// 4. Human approves via webhook// 5. Agent polls or receives eventWhat’s Next
Section titled “What’s Next”Start MITM’ing:
- hoody-exec Documentation → - Deploy custom MITM proxy scripts
- Script Execution → - API reference for hoody-exec Related Concepts:
- The HTTP Revolution → - Why HTTP enables MITM capabilities
- Security Model → - How MITM fits into Hoody’s security
- Hoody AI Overview → - Understanding the AI gateway architecture