In SDK , sub agent token calculation

Cursor SDK

Sub agents token calculation missing in run.usage

If i run a / (slash command or agent or skill) in a sdk and that skill/agent/command has sub agent routines as part of tasks. run.usage after the agent is completed doesnt provide the token consumed by the sub agents. Is there a way to get the overall token usage including the task in agent run ?

Operating System

MacOS

Version Information

cursor/sdk 1.0.24

Hi @Aadhithya_Manivannan Thanks for the forum post, I’ll break down the answers as best I can here:

Short answer: not from run.usage today. It only counts the main conversation’s turns, so any work a subagent does is missing from it. result.usage and the per turn usage stream event read from the same place, so all three undercount the same way. Those tokens are metered and billed normally, they just aren’t reported back through the SDK.

Worth knowing so you don’t go down that path: you can’t rebuild the total from onDelta either. Subagent activity does stream through as nested task updates, but the subagent’s turn end frame, which is the one carrying the token counts, is filtered out of that nested view.

What does work today: the team Admin API. Every usage event it returns carries a conversationId, and for SDK runs that value is exactly the agentId you already have. Subagent requests are recorded against their parent’s conversationId, so filtering on that one id gives you everything, tasks included.

const run = await agent.send("/your-command");
await run.wait();

const res = await fetch("https://api.cursor.com/teams/filtered-usage-events", {
  method: "POST",
  headers: {
    Authorization: `Basic ${Buffer.from(`${TEAM_API_KEY}:`).toString("base64")}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    startDate: runStartedAt,
    endDate: Date.now(),
    email: "[email protected]",
    pageSize: 1000,
  }),
});

const { usageEvents } = await res.json();

const total = usageEvents
  .filter((e) => e.conversationId === agent.agentId)
  .reduce((sum, { tokenUsage: t }) => sum + (t
    ? t.inputTokens + t.outputTokens + t.cacheReadTokens + t.cacheWriteTokens
    : 0), 0);

Four things to plan around:

  • It needs a team API key, so ask an admin to issue one if you don’t have admin access.
  • There’s no conversationId request filter yet, which is why the example filters client side.
  • Usage events aggregate hourly, so give a run some time before reconciling it.
  • The field list in the endpoint docs doesn’t mention conversationId yet, but it is returned today.

Making subagent usage attributable directly from the SDK is something we’re tracking, and I have your report linked to our backlog item so I can let you know any updates on it.

@kevinn Thanks for your quick response. I’ll try implementing your solution using the Team Admin API.

Please keep this thread updated or provide a thread where I can follow the feature update on the SDK for sub-agent token usage.