AutoGen Tutorial (TypeScript): adding cost tracking for intermediate developers
This tutorial shows you how to add token and dollar cost tracking to an AutoGen TypeScript agent run. You need this when you want per-request observability, budget enforcement, or simple chargeback for teams using LLM agents.
What You'll Need
- •Node.js 18+ and npm
- •A TypeScript project with
tsconfig.json - •AutoGen packages:
- •
@autogen/core - •
@autogen/openai
- •
- •An OpenAI API key in
OPENAI_API_KEY - •Basic familiarity with AutoGen agents and model clients
- •A place to persist logs, such as stdout, a file, or your app telemetry pipeline
Step-by-Step
- •Start with a minimal AutoGen setup that can emit usage data. The key piece is the model client: if the provider returns token usage, you can calculate cost after each run.
import { AssistantAgent } from "@autogen/core";
import { OpenAIChatCompletionClient } from "@autogen/openai";
const modelClient = new OpenAIChatCompletionClient({
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY,
});
const agent = new AssistantAgent({
name: "support_agent",
modelClient,
systemMessage: "You are a concise support assistant.",
});
- •Define a pricing table for the models you use. Keep it in code or config so you can update it without touching agent logic.
type Pricing = {
inputPer1M: number;
outputPer1M: number;
};
const pricingByModel: Record<string, Pricing> = {
"gpt-4o-mini": {
inputPer1M: 0.15,
outputPer1M: 0.6,
},
};
function calculateCostUsd(
model: string,
inputTokens: number,
outputTokens: number,
): number {
const pricing = pricingByModel[model];
if (!pricing) throw new Error(`No pricing configured for ${model}`);
const inputCost = (inputTokens / 1_000_000) * pricing.inputPer1M;
const outputCost = (outputTokens / 1_000_000) * pricing.outputPer1M;
return inputCost + outputCost;
}
- •Wrap your agent call so you can capture usage and compute the cost immediately after the response. In production, this is where you would also attach request IDs, user IDs, and tenant IDs.
async function main() {
const result = await agent.run("Write a short reply to a customer asking about reset links.");
const usage = result.usage;
if (!usage) {
throw new Error("Model usage was not returned by the provider");
}
const inputTokens = usage.promptTokens ?? usage.inputTokens ?? 0;
const outputTokens = usage.completionTokens ?? usage.outputTokens ?? 0;
const costUsd = calculateCostUsd(
"gpt-4o-mini",
inputTokens,
outputTokens,
);
console.log({
response: result.messages.at(-1)?.content,
inputTokens,
outputTokens,
totalTokens: inputTokens + outputTokens,
costUsd: Number(costUsd.toFixed(6)),
});
}
main().catch(console.error);
- •If you want reusable tracking across multiple calls, move the logic into a small helper. This keeps your business code clean and makes it easy to swap stdout logging for Prometheus, Datadog, or a database table later.
type CostRecord = {
model: string;
inputTokens: number;
outputTokens: number;
};
function trackCost(record: CostRecord) {
const costUsd = calculateCostUsd(
record.model,
record.inputTokens,
record.outputTokens,
);
return {
...record,
totalTokens: record.inputTokens + record.outputTokens,
costUsd: Number(costUsd.toFixed(6)),
timestamp: new Date().toISOString(),
};
}
- •Use the helper inside your request handler or workflow step. This is the pattern you want when one API request fans out into multiple agent calls and you need totals per user action.
async function runWithTracking(prompt: string) {
const result = await agent.run(prompt);
const usage = result.usage;
if (!usage) throw new Error("Missing token usage");
const tracked = trackCost({
model: "gpt-4o-mini",
inputTokens: usage.promptTokens ?? usage.inputTokens ?? 0,
outputTokens: usage.completionTokens ?? usage.outputTokens ?? 0,
});
console.log(JSON.stringify(tracked));
return {
text: result.messages.at(-1)?.content ?? "",
tracked,
};
}
Testing It
Run the script with a real prompt and confirm that the output includes inputTokens, outputTokens, totalTokens, and costUsd. If usage is missing, your provider/client configuration is not returning token metadata, so fix that before wiring this into billing or alerting.
Then test two prompts of different sizes and verify that the larger prompt costs more. For sanity checking, compare your computed numbers against the model’s published pricing table and make sure your math matches per-million-token billing.
If you’re using this in an API service, log one record per request and confirm that retries do not double count costs unless they actually trigger another model call. That distinction matters when you start enforcing budgets.
Next Steps
- •Add per-user and per-tenant aggregation so you can enforce monthly budgets
- •Store cost records in Postgres or ClickHouse for reporting
- •Track tool-call counts alongside token spend so you can spot expensive workflows
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