AI ORCHESTRATOR
ai-orchestrator.ts
Agent loop, tool use, retry, circuit breaker, fallback.
StarkWHAT THIS PATTERN TEACHES
Three patterns for coordinating AI model calls: simple completion with structured output, agent loops with tool-use, and circuit breakers to prevent cascading AI failures. Always set MAX_ITERATIONS on agent loops.
WHEN TO USE THIS
Any application that calls LLM APIs — from single completions to multi-step agent workflows.
AT A GLANCE
async function agentLoop(messages: Message[]) {
for (let i = 0; i < MAX_ITERATIONS; i++) {
const response = await model.complete(messages);
if (response.stopReason === 'end_turn') return;
// Execute tool calls, append results
}
}FRAMEWORK IMPLEMENTATIONS
TypeScript
const MAX_ITERATIONS = 10;
async function agentLoop(messages: Message[]): Promise<string> {
for (let i = 0; i < MAX_ITERATIONS; i++) {
const response = await model.complete({ messages });
if (response.stopReason === 'end_turn') {
return response.content;
}
for (const toolCall of response.toolCalls) {
const result = await executeTool(toolCall);
messages.push({ role: 'tool', content: result });
}
}
throw new Error('Agent loop exceeded max iterations');
}