All insights

Inference economics

How to Attribute Qwen3.8 Agent Costs Across Requests, Tools, and Sub-Agents

The best way to attribute Qwen3.8 agent costs is to use hierarchical distributed tracing: give the parent request one stable trace ID, represent every model invocation, tool execution, retry, and sub-agent as a span, and attach each direct cost to the span that incurred it. Then calculate exclusive cost for each span’s own activity and inclusive cost for that span plus its descendants. This preserves agent-level visibility, supports request-level rollups, and avoids counting child costs twice.

The best way to attribute Qwen3.8 agent costs is to use hierarchical distributed tracing: give the parent request one stable trace ID, represent every model invocation, tool execution, retry, and sub-agent as a span, and attach each direct cost to the span that incurred it. Then calculate exclusive cost for each span’s own activity and inclusive cost for that span plus its descendants. This preserves agent-level visibility, supports request-level rollups, and avoids counting child costs twice.

This method deliberately separates usage measurement from financial attribution. Token counts describe model consumption, but the total economic cost of an agent request may also include external tool fees, private compute, idle capacity, and shared serving overhead. For Qwen3.8 or any other model designation, teams should apply the billing rules of the actual provider or deployment rather than assuming a universal price, cache policy, or token-accounting method.

The Recommended Attribution Method in Brief

A production attribution model should answer two different questions:

  1. What directly consumed resources or incurred a charge?
  2. Which parent request, agent, product, team, or customer ultimately caused that activity?

The trace hierarchy answers the second question without weakening the first. Direct usage stays attached to the operation that generated it, while parent-child relationships allow finance and engineering teams to aggregate that usage at different levels.

Use one stable trace ID and a span for every cost-generating operation

Create a root span when the user, application, or upstream service initiates an agent request. Every subsequent operation should receive its own span ID and retain a relationship to that root.

A representative hierarchy might look like this:

``text Trace: customer-request-123 └── Root agent request ├── Qwen3.8 model invocation ├── Tool call: enterprise search │ └── External search execution ├── Sub-agent: document analyst │ ├── Model invocation │ └── Tool call: document retrieval ├── Sub-agent: response reviewer │ └── Model invocation └── Final Qwen3.8 model invocation ``

The model invocation that decides to call a tool may consume input and output tokens. Those orchestration tokens belong to the model span. If the external tool charges a fee or uses separately metered compute, that cost belongs to a child tool span. Treating the tool’s execution as model token usage would blur two distinct cost drivers.

A sub-agent should similarly receive a child span or trace-linked subtree. Its model calls, tools, and retries remain attributable to the sub-agent while the initiating request remains available for higher-level rollup.

Attach direct costs locally and roll them up recursively

Each span should contain only the usage and direct financial components created by that operation. Do not copy a child span’s cost into the parent’s direct-cost field.

A useful cost taxonomy is:

  • Input-token cost
  • Output-token cost
  • Cached-token cost, when separately reported
  • Other model API charges
  • External tool fees
  • Direct compute cost or consumption
  • Allocated shared infrastructure overhead

Keep these components separate even if dashboards also show a combined total. This lets teams distinguish, for example, a prompt-growth problem from excessive tool use or low private-inference utilization.

The basic rollup is:

```text exclusive_cost(span) = sum(direct cost components recorded on span)

inclusive_cost(span) = exclusive_cost(span) + sum(inclusive_cost(each direct child span)) ```

Calculate the root request’s total once from the hierarchy. Do not sum the inclusive cost of every span in a trace, because each parent’s inclusive value already contains its descendants.

For organizational reporting, the same immutable usage can be grouped by dimensions such as application, tenant, environment, agent type, model, provider, project, or cost center. These groupings should reference the underlying spans rather than rewriting them.

Report both exclusive and inclusive cost

Exclusive cost shows what a span incurred directly. It is useful for identifying expensive model calls, tools, and retries.

Inclusive cost shows the economic footprint of an operation and all work it initiated. It is useful for answering questions such as:

  • What did this customer request cost end to end?
  • How much did the research sub-agent contribute?
  • Which parent workflow generated the most downstream tool activity?
  • Did routing work to another agent reduce one span’s cost while increasing the full request cost?

For example, suppose a parent-agent span has $0.02 of direct activity, a model child has $0.08, and a tool child has $0.05. The parent’s exclusive cost is $0.02, while its inclusive cost is $0.15. The child costs should not also be added to that $0.15 when calculating the trace total.

These figures are illustrative only; they are not Qwen3.8 prices.

Build a Trace Hierarchy That Preserves Every Cost Relationship

The trace hierarchy should model causality, not merely execution order. Parallel branches may start at nearly the same time, asynchronous sub-agents may finish after the initiating process returns, and retries may repeat an operation. Parent-child relationships and explicit links preserve those distinctions better than a flat usage log.

Parent requests, model invocations, tool calls, and sub-agent subtrees

Use a root span for the business request and separate child spans for the operations it initiates. A practical span taxonomy includes:

  • agent_request for the root workflow or an agent’s delegated task
  • model_invocation for each call to a model endpoint or serving layer
  • tool_call for an external service, database, retrieval system, or internal function
  • retry or a retry-attempt attribute for repeated execution
  • sub_agent for delegated agent work
  • infrastructure_allocation when shared serving costs need an explicit accounting record

A model span should capture its own token categories and model charge inputs. A tool span should capture the tool’s fee, execution time, or compute consumption. A sub-agent span should own its direct orchestration activity and include its descendants through recursive rollup.

Asynchronous work requires particular care. If the original trace remains active, retain the same trace ID. If the execution system starts a separate trace, store both a trace link and a stable root-attribution ID. This allows delayed work to be charged to the initiating request without presenting it as a synchronous child.

Fields to capture on every span

The exact telemetry format can vary, but a useful attribution record includes the following fields:

```yaml trace_id: stable identifier for the originating request span_id: unique identifier for this operation parent_span_id: immediate causal parent, when applicable root_attribution_id: stable business request identifier linked_trace_ids: asynchronous or cross-system relationships

agent_id: logical agent or agent role operation_type: agent_request | model_invocation | tool_call | retry model: model designation used for the invocation provider: endpoint or serving provider service: application or tool that executed the operation

started_at: operation start timestamp ended_at: operation end timestamp status: success | failed | cancelled | timed_out attempt_number: retry sequence execution_mode: synchronous | parallel | asynchronous | speculative

input_tokens: provider-reported or locally measured value output_tokens: provider-reported or locally measured value cached_tokens: value when available and relevant cache_status: hit | miss | partial | unavailable tool_usage: tool-specific unit or execution measurement compute_usage: measured GPU, CPU, memory, or duration data

pricing_version: effective-dated pricing reference allocation_policy_version: shared-cost policy reference currency: reporting currency direct_cost_components: itemized financial amounts ```

Not every provider exposes every usage category. Use an explicit unavailable or not_reported state instead of converting missing data to zero. A zero means no usage or cost occurred; an unavailable value means it could not be measured from the available records.

Preserve the provider’s raw usage response alongside normalized fields where operationally appropriate. Normalization makes cross-provider reporting easier, while the raw record supports later investigation when billing definitions or parsers change.

Handle Retries, Failures, Parallel Work, and Cancellations Explicitly

Agent workflows often incur costs without producing a successful final answer. Excluding that work would understate operating cost and hide reliability problems.

  • Retries: Record every attempt as a separate span or separately identified attempt. Charge the usage actually incurred, including failed attempts, while keeping the logical operation ID consistent for analysis.
  • Failed calls: Retain any reported tokens, tool fees, or compute consumption. A failed outcome does not necessarily mean zero cost.
  • Parallel branches: Attribute each branch independently. Wall-clock overlap should not cause direct API charges to be divided, although shared private infrastructure may require a separate allocation method.
  • Speculative work: Label speculative spans and charge completed consumption even when their outputs are discarded.
  • Cancellations: Record whether cancellation occurred before execution, during generation, or after a downstream operation had started. Apply the provider’s or infrastructure system’s measured usage rather than assuming the call was free.
  • Asynchronous sub-agents: Preserve the initiating root-attribution ID and include their costs in the parent’s reporting window once they complete.

Because asynchronous costs may arrive late, define a close policy for financial reporting. For example, an operational dashboard can show provisional totals, while a later settlement process incorporates delayed tool invoices or completed background agents. The policy should state how late-arriving records affect previously reported periods.

Make Historical Cost Calculations Reproducible

Store immutable raw usage events separately from calculated financial records. Prices, currencies, provider terms, and internal allocation policies can change; overwriting the source usage makes historical recalculation difficult.

Use an effective-dated pricing table containing the provider, model or endpoint identifier, region when relevant, currency, usage category, unit price, and validity period. Each calculated cost record should reference the pricing-table version used.

``text calculated_cost = price(raw_usage, pricing_version) + tool_charge(raw_tool_usage, tool_price_version) + allocated_overhead(allocation_policy_version) ``

This separation is especially important when evaluating Qwen3.8 across different access routes. A managed endpoint and a private deployment may expose different metering units and cost structures. Before applying a price, verify the precise provider, endpoint, effective date, currency, region, cache treatment, and billing conditions.

Version the allocation policy as well as the price. If finance changes the treatment of idle GPU capacity, teams should be able to recalculate prior periods under the old or new policy without modifying the underlying inference records.

Allocate Shared Private-Inference Costs Without Hiding Assumptions

Private inference introduces costs that may not map directly to a single model span. GPU time, batching, idle capacity, scheduling overhead, and reserved infrastructure can serve several requests simultaneously.

There is no universally correct allocation method. Common options include:

  • Measured active compute: Allocate according to observed accelerator time or another direct consumption signal.
  • Token-weighted allocation: Divide shared cost according to normalized token volume, with appropriate treatment for different workloads.
  • Batch participation: Allocate a batch’s measured cost among the requests included in that batch.
  • Capacity-based allocation: Assign reserved or idle capacity to the team, tenant, application, or environment responsible for the reservation.
  • Hybrid allocation: Attach measured variable consumption to spans while distributing idle and platform overhead through a separate policy.

Whichever method is selected, label allocated overhead separately from direct model and tool charges. A request-level report might therefore show direct variable cost, allocated shared serving cost, and total attributed cost as three distinct values.

This distinction also improves decision-making. High direct model cost suggests different action from high idle-capacity allocation. The former may prompt teams to examine prompts, model selection, routing, or cache behavior; the latter may point toward scheduling, workload consolidation, or capacity planning.

Connect Attribution to Serving-Layer Cost Control

Attribution is most useful when it informs operating decisions. Once model, tool, sub-agent, and shared infrastructure costs remain distinguishable, teams can evaluate serving-layer controls with clearer before-and-after measurements.

Potential areas to examine include:

  • Caching: Compare cache-eligible usage, observed cache behavior when available, and downstream cost by workflow.
  • Routing: Evaluate inclusive request cost and outcome metrics when different models or serving paths handle the same class of work.
  • Batching: Measure batch utilization and request-level allocations without losing visibility into individual business requests.
  • Quantization: Evaluate infrastructure economics alongside workload-specific quality and reliability testing.
  • GPU scheduling: Examine active compute, queueing, idle capacity, and allocated overhead as separate operational signals.

Avoid evaluating these controls through token price alone. A lower-cost model call can still increase total request cost if it triggers more retries, longer workflows, or additional tools. Inclusive trace cost provides the end-to-end view, while exclusive span cost shows where the change occurred.

Token Forge Cloud focuses on inference cost control at the serving layer. Token Forge Cloud Private LLM Inference is designed for organizations evaluating private deployment and serving-layer optimization across areas such as caching, routing, batching, quantization, and GPU scheduling. The appropriate attribution and allocation design depends on the workload, infrastructure arrangement, and financial reporting policy.

For teams still validating model demand, Token Forge Cloud Managed Model APIs provides an API-first route to model access and usage data, with a path toward private deployment as workloads become predictable. Teams should confirm the required model endpoint, telemetry fields, and billing definitions for their intended deployment before finalizing a per-agent accounting design.

A Practical Implementation Sequence

A phased rollout reduces the risk of building financial reports on incomplete telemetry:

  1. Define the root business request and stable attribution identifier.
  2. Instrument model calls, tools, retries, and sub-agents as distinct spans.
  3. Store immutable raw usage and status records.
  4. Separate token, tool, compute, and overhead categories.
  5. Add effective-dated pricing and allocation-policy versions.
  6. Calculate exclusive and inclusive cost recursively.
  7. Test edge cases involving failure, cancellation, parallelism, and asynchronous work.
  8. Reconcile calculated totals with provider invoices or private-infrastructure costs at an agreed reporting level.
  9. Use the resulting data to evaluate routing and serving-policy changes.

Start with trace integrity before attempting detailed cost optimization. If parent-child relationships are incomplete, additional pricing precision will not restore the missing causal context.

Next Step

Contact Token Forge Cloud to discuss API access, private deployment, and LLM inference cost control.

Contact us