AutoGen Tutorial (TypeScript): implementing retry logic for intermediate developers
This tutorial shows you how to add retry logic around AutoGen agent calls in TypeScript, so transient model failures don’t break your workflow. You’ll wire in bounded retries with backoff, handle rate limits and timeouts, and keep your agent loop predictable under load.
What You'll Need
- •Node.js 18+ and npm
- •A TypeScript project with
ts-nodeortsx - •
@microsoft/autogen - •
openai - •An OpenAI API key in
OPENAI_API_KEY - •Basic familiarity with AutoGen agents, messages, and async/await
Step-by-Step
- •Start with a minimal TypeScript project and install the packages you need. Keep the runtime simple so the retry logic is easy to test and reuse.
npm init -y
npm install @microsoft/autogen openai
npm install -D typescript tsx @types/node
npx tsc --init
- •Create a small helper that retries only on transient failures. The important part is to fail fast on bad input, but retry on rate limits, timeouts, and temporary network issues.
// retry.ts
export async function withRetry<T>(
fn: () => Promise<T>,
maxAttempts = 3,
baseDelayMs = 500
): Promise<T> {
let lastError: unknown;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt === maxAttempts) break;
const message = error instanceof Error ? error.message : String(error);
const retryable =
message.includes("rate limit") ||
message.includes("timeout") ||
message.includes("ECONNRESET") ||
message.includes("ETIMEDOUT");
if (!retryable) throw error;
const delay = baseDelayMs * Math.pow(2, attempt - 1);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
throw lastError;
}
- •Build an AutoGen assistant agent and wrap its chat call with the retry helper. This keeps the retry policy outside the agent itself, which is cleaner when you have multiple agents or different reliability requirements per workflow.
// index.ts
import { AssistantAgent } from "@microsoft/autogen";
import OpenAI from "openai";
import { withRetry } from "./retry";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const assistant = new AssistantAgent({
name: "support_assistant",
modelClient: client,
systemMessage: "You are a concise support assistant.",
});
async function main() {
const result = await withRetry(() =>
assistant.run([
{ role: "user", content: "Summarize why retries matter in agent workflows." },
])
);
console.log(result);
}
main().catch((error) => {
console.error("Workflow failed:", error);
process.exit(1);
});
- •If you want more control, add a timeout wrapper so requests don’t hang forever. In production systems, retries without timeouts just move the failure from one place to another.
// timeout.ts
export function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
return Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => reject(new Error(`timeout after ${timeoutMs}ms`)), timeoutMs)
),
]);
}
- •Combine timeout and retry for a production-friendly call path. This pattern works well when your model provider occasionally slows down or returns bursty errors during peak traffic.
// index.ts
import { AssistantAgent } from "@microsoft/autogen";
import OpenAI from "openai";
import { withRetry } from "./retry";
import { withTimeout } from "./timeout";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const assistant = new AssistantAgent({
name: "support_assistant",
modelClient: client,
systemMessage: "You are a concise support assistant.",
});
async function main() {
const result = await withRetry(() =>
withTimeout(
assistant.run([
{ role: "user", content: "Give me one sentence about exponential backoff." },
]),
10_000
)
);
console.log(result);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
Testing It
Run the script normally first and confirm you get a response from the agent. Then temporarily break your API key or disconnect your network to see that non-retryable failures fail immediately while transient errors trigger the backoff path.
To verify the timing behavior, log each attempt inside withRetry and confirm delays increase between attempts. If you want a stronger test, point the helper at a mocked function that throws twice before succeeding, then assert that it eventually returns the expected result.
Next Steps
- •Add jitter to your backoff so multiple workers don’t retry at the same time.
- •Classify errors by provider status codes instead of string matching.
- •Move retry policy into a shared utility used by all agents, tools, and HTTP clients in your stack.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit