ShowMerit

See Grok's Reasoning Tokens and Billed Cost on Each Trace in Splunk APM

An app on EKS calls grok-4.7, traced with OpenTelemetry into Splunk APM: xAI reports reasoning tokens apart from completion tokens. A span processor puts them and xAI's billed cost on each chat span; all 12 enriched traces matched xAI's numbers.

Rechecked 1 Dec 2026: works with splunk-otel-collector chart 0.163.1 and 0.165.0 (on a GitHub runner)

Abhimanyu BajajPublished 30 Nov 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-grok-s-reasoning-tokens-and-billed-cost-on-each-trace-in

I asked Grok a one-line question: a store sold 200 orders, but its dashboard says 400, so what's the most likely cause? It answered in one sentence, most often: "The dashboard is most likely double-counting each order." In one call the answer was 20 tokens, and the trace Splunk APM read back for it showed 166 reasoning tokens on top: Grok thinking before it answered, billed as output.

Those thinking tokens are called reasoning tokens, and you pay for them like any other output. This post puts them, and the exact cost of each call, on the OpenTelemetry trace in Splunk APM, next to the answer they paid for.

Splunk APM trace of one Grok call: the chat span shows gen_ai.usage.input_tokens 1242, gen_ai.usage.output_tokens 20 and gen_ai.usage.reasoning.output_tokens 166

One call in Splunk APM from the 30 Nov 2026 lab run: 20 output tokens, 166 reasoning tokens.

In plain words

What I tested

A small Python app on EKS asked grok-4.7 the same 21-word question 24 times: 6 times at reasoning effort low and 6 at high, alternating, then the same 12 again with the fix. The OpenTelemetry instrumentation traced every call and sent it through the Splunk OTel Collector to Splunk APM. For each call the app printed xAI's own usage, including the billed cost in cost_in_usd_ticks (10,000,000,000 ticks to the dollar, per xAI's docs), next to what the trace carried. Then the lab read every trace back from Splunk APM by its ID.

This is one model, one short prompt and 6 calls per effort. Your prompts will reason more or less.

What the chat span records

xAI reports reasoning tokens apart from completion tokens: in every call, total tokens = prompt + completion + reasoning. The instrumentation's gen_ai.usage.output_tokens is the completion tokens as the API reports them, so the reasoning sits outside it. Across the four groups of 6 calls, 91.2 to 95.9% of the output xAI billed was reasoning that output_tokens doesn't include.

Splunk APM trace of a call before the fix: the chat span has gen_ai.usage.input_tokens 1242 and gen_ai.usage.output_tokens 27, and no reasoning attribute

Before enrichment: 27 output tokens on the span. xAI's usage for this call reported 246 reasoning tokens.

Group (6 calls) Completion tokens, avg Reasoning tokens, avg (min to max) Billed, USD per 1,000 calls
plain, low effort 18.7 199 (120 to 261) 2.062
plain, high effort 14.2 331 (248 to 435) 2.827
fixed, low effort 15.2 189 (166 to 215) 1.981
fixed, high effort 16 264.8 (192 to 455) 2.441

The low and high ranges overlapped in both runs, so these 12 calls per run don't show that high effort costs more. A bigger sample would be needed to test that.

Two surprises in the bill

A 21-word question was billed as 1,242 prompt tokens. Every call reported 1,242 prompt tokens, and 1,152 of them were cached. The lab didn't find where the extra tokens come from, so check prompt_tokens on your own calls.

Cached tokens are cheaper, and that changes any estimate. The lab priced each call's usage at the list prices its script uses for grok-4.7, $2 per million input tokens and $6 per million output, with reasoning priced as output. Priced that way, 0 of 24 calls matched the bill within 1%, because every call had 1,152 cached prompt tokens and the script has no cached-input price. So the lab worked the cached price out from each bill: (billed cost minus uncached input and output at list prices) divided by the cached tokens. It printed "12 calls, avg 0.5, min 0.5, max 0.5" for each run: $0.50 per million cached tokens, the same in all 24 calls.

So an estimate built from the span alone is wrong twice: it prices all 1,242 input tokens at full price, and it leaves out the reasoning. Here the two errors pull in opposite directions. The span-only estimate came out at $2.569 to $2.596 per 1,000 calls in every group, while the bill ran from $1.981 to $2.827. Change the prompt or the effort and the gap moves.

Enrich the chat span with xAI's numbers

OpenTelemetry already has an attribute for this: gen_ai.usage.reasoning.output_tokens. xAI already returns the billed cost. A small span processor holds each chat span until the response is back, then adds both (trimmed from the lab's script, where the plain run lets every span pass straight through):

class AddUsage(SpanProcessor):
    """Holds each chat span until the app has xAI's response, then exports it with
    the response's reasoning tokens and billed cost added."""
    def __init__(self, delegate):
        self.delegate, self.held = delegate, {}
    def on_start(self, span, parent_context=None):
        self.delegate.on_start(span, parent_context=parent_context)
    def on_end(self, span):
        if span.name.startswith("chat"):
            self.held[span.context.trace_id] = span
            return
        self.delegate.on_end(span)
    def add(self, trace_id, extra):
        s = self.held.pop(trace_id, None)
        if s is None:
            return
        self.delegate.on_end(ReadableSpan(name=s.name, context=s.context, parent=s.parent,
            resource=s.resource, attributes={**(s.attributes or {}), **extra}, events=s.events,
            links=s.links, kind=s.kind, status=s.status, start_time=s.start_time,
            end_time=s.end_time, instrumentation_scope=s.instrumentation_scope))
    def shutdown(self):
        for s in self.held.values():          # export anything still held, then flush
            self.delegate.on_end(s)
        self.held = {}
        self.delegate.shutdown()
    def force_flush(self, timeout_millis=30000):
        return self.delegate.force_flush(timeout_millis)

After each call, the app passes in what the response said:

usage.add(trace_id, {"gen_ai.usage.reasoning.output_tokens": reasoning,
                     "xai.usage.cost_usd": ticks / 10_000_000_000})

gen_ai.usage.reasoning.output_tokens is the OpenTelemetry attribute, and it's what makes this possible. xai.usage.cost_usd is my own name for xAI's billed cost, in dollars. It isn't an OpenTelemetry attribute.

Before you use this in production: the lab makes one call per trace and one call at a time. Key the held spans by the chat span's own span ID (or put it in a context variable) if a trace makes several calls, guard the dictionary with a lock if calls run concurrently, and export any held span that never gets its response after a timeout, so nothing piles up in memory.

What Splunk APM shows now

The lab read all 24 traces back from Splunk APM. In the 12 fixed calls, every chat span carried both attributes, each equal to what xAI's response said for that call. None of the 12 plain chat spans had either one.

Splunk APM span tags for the same call: span.kind client and xai.usage.cost_usd 0.001872

The same call's billed cost on its span: $0.001872.

Screenshot of the xAI console's Logs page

The xAI console's Logs page.

The same numbers on a Splunk dashboard

A trace shows one call at a time. To see a whole run at once, I ran the lab a second time with the app also sending its own usage metrics to Splunk: tokens by type, the billed cost and the number of calls, each taken from xAI's response. The second run made its own 24 calls, so its numbers differ a little from the ones above.

Splunk dashboard of the second run: stacked columns of reasoning and answer tokens, a single value of 94.3 percent reasoning share, and billed cost per 1,000 calls of 1.903 dollars at low effort and 2.990 dollars at high

The second run in Splunk. Each column is a 5-second window: reasoning tokens in pink, the answer in blue. Reasoning was 94.3% of billed output, and 1,000 calls would cost $1.903 at low effort and $2.990 at high.

The dashboard's numbers match the run's own per-call figures; the sums are at the end of its log (evidence). The app sends the metrics as delta counters, every 5 seconds; the code is in k8s/20-asker.yaml.

Try it yourself

What runs: EKS 1.34 with one t3.large node, the Splunk OTel Collector chart 0.163.1, and one Python 3.13 pod with openai 3.60.0, opentelemetry-instrumentation-openai-v2 2.4b0 and opentelemetry-util-genai 1.1b0, sending traces over OTLP/HTTP to the Splunk agent on its node.

git clone https://github.com/Abhimanyu9988/showmerit
cd showmerit/labs/see-grok-s-reasoning-tokens-and-billed-cost-on-each-trace-in
export SPLUNK_REALM=us1 SPLUNK_ACCESS_TOKEN=... SPLUNK_API_TOKEN=... LAB_NAME=grok-lab
export XAI_API_KEY=...   # a key with a spend cap
export AWS_REGION=eu-central-1   # with AWS credentials already configured
terraform init && terraform apply
./reproduce.sh   # plain: the instrumentation only
./fix.sh         # the span processor, then every trace read back from Splunk APM
./cleanup.sh && terraform destroy   # remove what the lab created

Use a separate xAI key with a spending limit, and pass it to the pod as a Kubernetes secret, the way reproduce.sh does. After my run, cleanup.sh and terraform destroy removed everything the lab created (17 resources).

What to take away

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

I work at Splunk, part of Cisco. Views are my own.

Read next

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