AutoGen Tutorial (TypeScript): adding cost tracking for beginners

By Cyprian AaronsUpdated 2026-04-21
autogenadding-cost-tracking-for-beginnerstypescript

This tutorial shows you how to add cost tracking to an AutoGen TypeScript agent workflow, so you can measure token usage and estimate spend per run. You need this when you’re building anything that can call models repeatedly, because “it worked” is not enough if the bill is unpredictable.

What You'll Need

  • Node.js 18+ installed
  • A TypeScript project with AutoGen installed
  • An OpenAI API key exported as OPENAI_API_KEY
  • npm or pnpm
  • Basic familiarity with:
    • AssistantAgent
    • UserProxyAgent
    • running a simple AutoGen chat loop

Install the packages:

npm install @autogenai/autogen @autogenai/core
npm install -D typescript tsx @types/node

Step-by-Step

  1. Start with a minimal AutoGen setup that can run a chat and return usage data. The important part is using an OpenAI model configuration that supports token accounting.
import { AssistantAgent, UserProxyAgent } from "@autogenai/autogen";
import { OpenAIChatCompletionClient } from "@autogenai/openai";

async function main() {
  const modelClient = new OpenAIChatCompletionClient({
    model: "gpt-4o-mini",
    apiKey: process.env.OPENAI_API_KEY,
  });

  const assistant = new AssistantAgent({
    name: "assistant",
    modelClient,
    systemMessage: "You are a concise assistant.",
  });

  const user = new UserProxyAgent({
    name: "user",
  });

  const result = await user.initiateChat(assistant, {
    message: "Summarize why cost tracking matters in agent workflows.",
  });

  console.log(result);
}

main();
  1. Read the usage metadata from the chat result. In AutoGen, the exact shape can vary by client version, but the goal is the same: capture prompt tokens, completion tokens, and total tokens after each run.
import { AssistantAgent, UserProxyAgent } from "@autogenai/autogen";
import { OpenAIChatCompletionClient } from "@autogenai/openai";

async function main() {
  const modelClient = new OpenAIChatCompletionClient({
    model: "gpt-4o-mini",
    apiKey: process.env.OPENAI_API_KEY,
  });

  const assistant = new AssistantAgent({
    name: "assistant",
    modelClient,
  });

  const user = new UserProxyAgent({ name: "user" });

  const result = await user.initiateChat(assistant, {
    message: "Give me three reasons to track LLM costs.",
  });

  console.log("Raw result:", JSON.stringify(result, null, 2));
}

main();
  1. Add a small cost calculator. For beginners, hardcoding prices is fine as long as you keep them in one place and update them when your provider changes pricing.
type Usage = {
  promptTokens?: number;
  completionTokens?: number;
};

const PRICING_PER_1M_TOKENS = {
  prompt: 0.15,
  completion: 0.6,
};

function estimateCost(usage: Usage) {
  const promptTokens = usage.promptTokens ?? 0;
  const completionTokens = usage.completionTokens ?? 0;

  const promptCost = (promptTokens / 1_000_000) * PRICING_PER_1M_TOKENS.prompt;
  const completionCost =
    (completionTokens / 1_000_000) * PRICING_PER_1M_TOKENS.completion;

  return {
    promptTokens,
    completionTokens,
    totalTokens: promptTokens + completionTokens,
    estimatedCostUsd: promptCost + completionCost,
  };
}
  1. Wrap your chat call so it logs usage every time. This is the pattern you want in production too: one function owns the request, one function owns the accounting.
import { AssistantAgent, UserProxyAgent } from "@autogenai/autogen";
import { OpenAIChatCompletionClient } from "@autogenai/openai";

type Usage = {
  promptTokens?: number;
  completionTokens?: number;
};

const PRICING_PER_1M_TOKENS = {
  prompt: 0.15,
  completion: 0.6,
};

function estimateCost(usage: Usage) {
  const promptTokens = usage.promptTokens ?? 0;
  const completionTokens = usage.completionTokens ?? 0;

  return {
    promptTokens,
    completionTokens,
    totalTokens: promptTokens + completionTokens,
    estimatedCostUsd:
      (promptTokens / 1_000_000) * PRICING_PER_1M_TOKENS.prompt +
      (completionTokens / 1_000_000) * PRICING_PER_1M_TOKENS.completion,
  };
}

async function main() {
  const modelClient = new OpenAIChatCompletionClient({
    model: "gpt-4o-mini",
    apiKey: process.env.OPENAI_API_KEY,
  });

   const assistant = new AssistantAgent({ name: "assistant", modelClient });
   const user = new UserProxyAgent({ name: "user" });

   const result = await user.initiateChat(assistant, {
     message: "Write a two-line explanation of token costs.",
   });

   // Adjust these property names if your client version exposes them differently.
   const usage = (result as any).usage ?? (result as any).tokenUsage ?? {};
   console.log("Usage:", estimateCost(usage));
}

main();
  1. If you want cost tracking across multiple runs, store each run’s metrics in an array and print a summary at the end. That gives you per-request visibility plus a simple total for the session.
type RunMetrics = {
  label: string;
};

const runs: Array<{
label: string;
promptTokens: number;
completionTokens: number;
totalTokens: number;
estimatedCostUsd: number;
}> = [];

function addRun(label: string, usage: { promptTokens?: number; completionTokens?: number }) {
const metrics = estimateCost(usage);
runs.push({ label, ...metrics });
return metrics;
}

function printSummary() {
const totalCost = runs.reduce((sum, r) => sum + r.estimatedCostUsd, .0);
console.table(runs);
console.log(`Session total USD: $${totalCost.toFixed(6)}`);
}

Testing It

Run the script with OPENAI_API_KEY set and confirm you get both an assistant response and a usage object printed to the terminal. If usage comes back empty, inspect the raw result first; different AutoGen versions expose token metadata under slightly different property names.

Then send two or three prompts of different lengths and compare the numbers. Longer prompts should increase prompt tokens, while more verbose answers should increase completion tokens.

If you see undefined for everything, check three things:

  • Your model client actually supports usage reporting
  • You are reading the right field from the returned object
  • You are using a real API key and not a mock client

Next Steps

  • Persist cost data to Postgres or SQLite instead of only logging it
  • Add per-user or per-workspace budgets and stop chats when limits are exceeded
  • Wrap this into middleware so every agent run gets tracked automatically

Keep learning

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

Related Guides