ShowMerit

See Where an AI Agent's Tokens Go: Every Grok Step in One Splunk APM Trace

A grok-4.7 agent on EKS, traced with OpenTelemetry into Splunk APM: invoke_agent and execute_tool spans made each question one trace with its total tokens and cost, and showed 4 or 5 model calls with the prompt growing 2.91x by the last.

Abhimanyu BajajPublished 1 Dec 20268 min readTested in eu-central-1Lab codeLab evidence

Run this yourself

On your AWS account

The full lab with your own AWS account and Splunk token. Follow the steps in this article, then run its clean up step so nothing keeps running.

git clone https://github.com/Abhimanyu9988/showmerit.git
cd showmerit/labs/see-where-an-ai-agent-s-tokens-go-every-grok-step-in-one

Two OpenTelemetry span types, invoke_agent and execute_tool, turned a Grok agent's separate model-call traces into one trace per question in Splunk APM: every model call and tool call as a step, and the task's total tokens and billed cost on top. That one view is where I could see the agent's tokens pile up: one question took 4 or 5 calls, and the prompt grew with every one.

Splunk APM trace view of one agent task: invoke_agent order-checker as the root span, with five chat grok-4.7 spans and four execute_tool spans (get_order_count twice, list_recent_deploys, get_webhook_log) under it

Second run, task 1 in Splunk APM: one trace, every model call and tool call as a step.

The question was a small on-call puzzle: "We sold 200 orders today, but the sales dashboard shows 400. Find out why." All 8 of the agent's answers named the same cause from its tools: a 09:12 checkout deploy made the order webhook retry every delivery, so each order counted twice.

One cause reported in agent tools for the "burning tokens" people describe is what this lab shows too: the conversation is resent on every call, and a large tool result joins it.

TL;DR

In plain words

What I tested

A Python pod on EKS ran the agent with grok-4.7 (reasoning effort low) and three tools with fixed data: an order count, today's deploys, and the order webhook's log. The OpenTelemetry OpenAI instrumentation (opentelemetry-instrumentation-openai-v2) traced every model call into Splunk APM through the Splunk OTel Collector. Each task started with its own ticket ID, so no task could reuse another task's messages. Two runs of 4 tasks: the instrumentation only, then with the agent spans.

What you'll see: every call bigger than the last

Here is the first run's first task, call by call, as xAI reported it:

Call Prompt tokens Cached New Completion + reasoning Tool it asked for Billed (ticks)
1 1,471 1,152 319 13 + 32 get_order_count 14,840,000
2 1,542 1,152 390 13 + 11 get_order_count 15,000,000
3 1,592 1,152 440 11 + 15 list_recent_deploys 16,120,000
4 1,779 1,536 243 13 + 41 get_webhook_log 15,780,000
5 4,284 1,664 2,620 38 + 63 (answer) 66,780,000

The lab's log puts this task at $0.012852 billed. Call 5 was 66,780,000 of its 128,520,000 ticks.

All 8 tasks looked alike: 4 or 5 calls, 4 tool calls, $0.011 to $0.0129 each, about $11.90 per 1,000 tasks. In the 4-call tasks, Grok asked for both order counts in one step, despite the one-tool-at-a-time instruction.

Where the tokens go

The model keeps no memory between calls. Each call sends the whole conversation again: instructions, question, earlier answers and tool results. In both runs, 58.3% of all prompt tokens had already been sent in an earlier call of the same task.

The prompt cache softens it. xAI reported 65.7% of all prompt tokens as cached in each run: 27,008 cached tokens out of 41,095 in the first and 41,123 in the second. The same cached total appeared in both runs; per task it differed. Even each task's first call had 1,152 cached tokens, though every task started with a fresh ticket ID. My guess is that this is something xAI places ahead of the messages, such as the tool definitions; I didn't test that.

The jump lines up with new tokens. After the webhook log came back, the first run's next call carried 2,611 to 2,623 new, uncached tokens. In the first task, that last call's 66,780,000 ticks were more than calls 1 to 4 together. Its output stayed small (38 completion and 63 reasoning tokens in the first task), so in this lab the jump in billed ticks came with the new input. On average the last call cost 4.45× the first in the first run and 4.42× in the second. The tools were local functions with fixed data; in this lab, the bill grew after a tool dropped a big result into the conversation.

xAI also counts reasoning apart from completion tokens: call 1 had 13 completion and 32 reasoning, and its chat span's output_tokens shows the 13.

What the instrumentation shows on its own

The instrumentation gives every model call a chat span with its tokens: the per-call view you need. With no span around the loop, each chat call starts its own trace, as you'd expect, so the first run's 4 tasks showed up as 19 traces.

Splunk APM Trace Analyzer for the order-agent service: four invoke_agent order-checker traces with 9 or 10 spans each, then single-span chat grok-4.7 traces from the first run

Splunk APM: the second run's 4 tasks on top, the first run's single-call traces below.

One trace per task: add the OpenTelemetry invoke_agent and execute_tool spans

The GenAI agent conventions define an invoke_agent span for the task and an execute_tool span per tool call. The instrumentation's chat spans then sit under the task:

def agent_span():
    return tracer.start_as_current_span(f"invoke_agent {AGENT}", kind=SpanKind.INTERNAL, attributes={
        "gen_ai.operation.name": "invoke_agent", "gen_ai.agent.name": AGENT, "gen_ai.provider.name": "x_ai",
        "gen_ai.request.model": MODEL})

def tool_span(name, call_id, step):
    return tracer.start_as_current_span(f"execute_tool {name}", kind=SpanKind.INTERNAL, attributes={
        "gen_ai.operation.name": "execute_tool", "gen_ai.tool.name": name, "gen_ai.tool.call.id": call_id,
        "gen_ai.tool.type": "function", "lab.step": step})

When the task ends, its totals go on the task's span:

aspan.set_attribute("gen_ai.usage.input_tokens", tot["prompt"])
aspan.set_attribute("gen_ai.usage.output_tokens", tot["billed_out"])
aspan.set_attribute("gen_ai.usage.cache_read.input_tokens", tot["cached"])
aspan.set_attribute("xai.usage.cost_usd", tot["ticks"] / TICKS_PER_USD)

xai.usage.cost_usd is my own name for xAI's billed cost, from the cost_in_usd_ticks field described in xAI's cost tracking docs; the gen_ai.* names are OpenTelemetry's.

Read back from Splunk APM: 4 tasks, 4 traces, all 16 tool calls as execute_tool spans, and the totals the app set on every root span arrived intact.

Splunk APM span tags on the invoke_agent order-checker span: gen_ai.usage.cache_read.input_tokens 7040, gen_ai.usage.input_tokens 10668, gen_ai.usage.output_tokens 257, lab.steps 5

Second run, task 1: its totals on the root span. Its prompts match the first run's task 1 call for call (10,668 tokens), but more was cached (7,040 against 6,656), so the bill differs.

Splunk APM span tags further down the same span: xai.usage.cost_usd 0.012318

Second run, task 1: billed cost.

Open the last chat span in the same trace (second run, task 1) and you see the jump: 4,284 input tokens, where the run's first calls averaged 1,470.3.

Splunk APM span tags on the task's last chat grok-4.7 span: gen_ai.usage.input_tokens 4284, gen_ai.usage.output_tokens 43

The answer call, after the webhook log joined the conversation.

Before you build a dashboard on this: input and cached tokens repeat between the root and its chat spans, so sum one span type, never all spans. For output, each chat span reports xAI's completion tokens as output_tokens, and xAI reports reasoning separately, so the lab sets the root's output_tokens to the task's completion plus reasoning tokens across all its calls (93 + 164 = 257 for this task). The xAI console's request log for the lab key is one more place to look.

xAI console Logs page listing the lab key's grok-4.7 requests

xAI's log for the lab key, second run.

Try it yourself

EKS 1.34 on one t3.large node, Splunk OTel Collector chart 0.163.1, Python 3.13 with openai 3.60.0 and opentelemetry-instrumentation-openai-v2 2.4b0:

git clone https://github.com/Abhimanyu9988/showmerit
cd showmerit/labs/see-where-an-ai-agent-s-tokens-go-every-grok-step-in-one
export SPLUNK_REALM=us1 SPLUNK_ACCESS_TOKEN=... SPLUNK_API_TOKEN=...
export LAB_NAME=agent-lab AWS_REGION=eu-central-1
export XAI_API_KEY=...   # a key with a spend cap
terraform init && terraform apply -var lab_name=agent-lab
./reproduce.sh   # the instrumentation only
./fix.sh         # the agent and tool spans, then every trace read back from Splunk APM
./cleanup.sh && terraform destroy -var lab_name=agent-lab

My run's 38 Grok calls were billed under ten cents. The raw output is in the lab's evidence folder.

What I didn't test

Every file in this lab

Everything above runs from these files. Open any of them to copy it, or clone the repo as shown:

Sources

Disclosure: I work at Splunk, part of Cisco. This is my personal blog; the post isn't sponsored or reviewed by my employer.


I reproduced this in a disposable AWS lab. The lab code and raw output are in the repo. Drafted with AI assistance and reviewed by me.

Read next

OpenTelemetry code owner for two Collector components. Writes hands-on AWS container guides, each tested in a lab. See the proof