Consider a workflow where an orchestrator agent breaks a research task into four sub-jobs, spawns a sub-agent for each one, and each sub-agent calls a different model provider. The orchestrator calls GPT-4o for synthesis. Sub-agent A calls Claude 3.5 Sonnet for extraction. Sub-agent B calls Gemini Flash for classification. Sub-agent C calls an internal fine-tuned model hosted on AWS. All of this runs under a single customer's account in a 90-second wall-clock window. By the time the pipeline finishes, you have events from four different model providers, each with its own token count, its own cost rate, and no shared identifier that naturally ties them together.
Getting the total token count right is the easy part. The challenge is attribution: which customer paid for this run, which workflow triggered it, and which sub-agent within that workflow generated which cost. Without that granularity, you cannot produce a meaningful invoice, you cannot build per-feature cost reporting, and you cannot answer the question every AI product founder eventually gets: "Why did my bill jump this month?"
What Attribution Actually Requires
Attribution in a multi-agent pipeline has three dimensions. The first is customer attribution: which end customer's account does this usage belong to? The second is workflow attribution: which orchestrated workflow, across all the customer's concurrent pipelines, triggered this specific sub-agent run? The third is agent attribution: within the workflow, which agent in the execution tree generated this event?
A minimal event payload that supports all three looks like this:
{
"idempotency_key": "evt_run_8821_subB_call_002",
"customer_id": "cust_acme_corp",
"workflow_id": "wf_research_20260903_0940",
"trace_id": "trc_8821",
"parent_agent_id": "agt_orchestrator_01",
"agent_id": "agt_classify_B",
"event_type": "model.completion",
"quantity": 1,
"metadata": {
"model": "gemini-flash-1.5",
"input_tokens": 1240,
"output_tokens": 88,
"cost_usd": 0.00031
}
}
The idempotency_key ensures that if the pipeline retries this call on a transient error, you do not count it twice. The workflow_id groups all events from a single pipeline execution together. The trace_id is the correlation identifier that lets you reconstruct the full execution tree. The parent_agent_id records the relationship: this sub-agent was spawned by the orchestrator, so its cost rolls up to the orchestrator's run.
Building the Execution Tree from Events
The parent_agent_id field is what makes tree reconstruction possible. Given a set of events for a single trace_id, you can build a directed graph where each agent is a node and each spawning relationship is an edge. Aggregate the event costs along that graph and you get cost rollups at every level: per sub-agent, per branch, per workflow, per customer.
This matters because cost distribution within a workflow is rarely uniform. In a research pipeline, the synthesis step might consume 70 percent of the total tokens simply because it processes the combined outputs of all the sub-agents. Without the tree structure, you know the total cost but you cannot tell the customer which part of their workflow is expensive or suggest which step to optimize first.
For products that want to expose per-feature cost reporting to their own customers, the tree is also the data model behind the dashboard. A customer asking "how much did my research pipeline cost versus my summarization pipeline?" is asking for an aggregation by workflow_id. A customer asking "which agent in my pipeline is consuming the most tokens?" is asking for an aggregation by agent_id within a trace. Both queries are trivial if the events carry the right fields. Both are impossible if they do not.
Where Attribution Goes Wrong
The most common failure is wrapping model calls at the infrastructure layer rather than at the workflow layer. If you instrument at the SDK level, capturing every OpenAI completion call, you get accurate token counts but you lose the workflow and agent context. The event knows which model was called and how many tokens it used, but it does not know which customer workflow triggered the call or which agent within that workflow was responsible.
This produces billing data that is correct at the macro level and useless at the detail level. You can generate an invoice total, but you cannot break it down by feature, by workflow, or by agent. When a customer disputes an item or asks for cost attribution by their own product feature, you have nothing to show them.
The second failure is propagating trace context inconsistently. If the orchestrator generates a trace_id but sub-agents do not inherit it, events from sub-agents appear as unrelated top-level items. The execution tree cannot be reconstructed. You end up with a collection of isolated events rather than a structured record of what the pipeline did.
The third failure is using opaque identifiers for customers and workflows. A customer_id of "user_8821" is fine if you have a mapping table. A customer_id of "cust_acme_corp" makes the event self-describing. When you are debugging a billing discrepancy at 11 PM, self-describing identifiers matter.
Carrying Context Across Agent Boundaries
Multi-agent frameworks handle context propagation differently. In LangChain and LangGraph, the trace context lives in a callback handler. In CrewAI, it can be passed as task metadata. In custom orchestrators built on queues, it typically travels as a header in the task payload. The specifics vary, but the pattern is the same: context must be an explicit first-class input to each agent, not something inferred from the calling context.
The practical implementation in Paygentic uses a simple propagation contract. When the orchestrator fires its first event, it generates a trace_id and includes it in the event payload. When it spawns a sub-agent, it passes the trace_id and its own agent_id as parent_agent_id into the sub-agent's initialization context. The sub-agent includes both in every event it fires. If the sub-agent spawns further agents, the chain extends.
The instrumentation cost is low: two additional fields per event. The attribution value is high: every event in a pipeline execution is linkable back to the triggering workflow and the spawning agent, regardless of how deeply nested the execution tree becomes.
Attribution Is Not Only a Billing Problem
We want to be clear about scope here. We are not saying that detailed attribution is required purely for billing accuracy. Token totals are accurate without it. What attribution enables is a different class of product decisions.
When you can attribute cost to workflows, you can build a cost efficiency report: which of your customer's workflows has the best output-to-token ratio? When you can attribute cost to agents, you can surface optimization opportunities: your summarization sub-agent is using a model that costs 8x as much as a smaller model that performs identically on this task class. These are product insights, not just billing line items. They are only available if the event data carries enough context to support the analysis.
Building attribution into the event schema at the start is much cheaper than adding it later. Once you have customers with production traffic, retrofitting a trace_id and parent_agent_id requires coordinated changes across your instrumentation layer, your metering configuration, and any existing invoice data. Doing it at the start is a one-time decision that costs two extra fields per event. Doing it later is a migration.