Reconciling one LangGraph run's costs across several paid services

A LangGraph job that calls a search API, a data API and an LLM ends up with three bills from three providers. I wanted one record per customer job that says what it cost, which node caused each charge, and what doesn’t add up, so I built a small example.

How it works:

  • Each node that calls a paid service appends a delegation to graph state: provider, the provider’s job reference (request id or run id), and the quote if there is one. The `delegations` list uses an `operator.add` reducer, so nodes only append.

- A final `reconcile` node imports the providers’ bills, matches each charge to its delegation by job reference, and produces a signed closure that verifies offline.

In the example the company-data API quotes USD 2.00 and bills USD 2.40. The closure keeps that as an open `amount_mismatch` exception rather than burying it in the total:

cd examples/langgraph-paid-services

python3 -m venv .venv && .venv/bin/pip install -r requirements.txt

.venv/bin/python graph.py

net cost USD 3.27 (charged USD 3.27)

open exceptions: 1

amount_mismatch: billed 240 USD exceeds quoted maximum 200

The services are deterministic stand-ins, so no API keys are needed (Python 3.10+, Node.js 20+).

For the job-reference matching, what happens if a provider accepts a paid request but the graph crashes before persisting the delegation? The imported bill would have a provider job reference with no saved delegation to match against.

Nice example. Keeping the 2.00 vs 2.40 gap as an open exception instead of folding it into the total is the right call.

On capitaharlock’s crash question: the append to graph state only gets persisted once the node returns and the checkpointer writes that step, so a crash during the paid call loses it. One way around it is to write the delegation as an intent record to an external store before the call (status pending, with your own job id), then update it with the provider’s reference afterwards. Where the provider accepts an idempotency key or metadata, pass that job id through so a charge with no saved delegation can still be matched back. Anything on a bill that matches nothing becomes its own exception type, same as amount_mismatch.

When I built multi-agent systems for clients we did this kind of reconciliation with spreadsheets and shell scripts, and the audit trail was always the part that fell apart. How are you handling providers whose bills land after the reconcile node has already run?

That gap is the classic dual-write problem, and the usual fix is to persist intent before the paid call rather than after. Have the node write a “pending” delegation with a client-generated idempotency key first, let the checkpointer commit it, then make the provider call in a separate step that passes the same key (many APIs accept a request id or a metadata field). If the graph dies mid-call, the reconcile node still finds a pending delegation and can query the provider by that key to recover the real job reference.

For providers that don’t accept a client key, treat unmatched bill lines as their own exception type, something like orphan_charge, rather than dropping them. Match them heuristically on a timestamp window, amount and the thread id you tagged in request metadata, and flag them for review instead of auto-closing.

It also helps to put each paid call in its own node, so resuming after a crash doesn’t re-run the expensive request. Checking for an existing completed delegation with that key at the start of the node makes the replay safe.

Nice example. Keeping the 2.00 vs 2.40 gap as an open exception instead of folding it into the total is the right call.

On capitaharlock’s crash question: the append to graph state only gets persisted once the node returns and the checkpointer writes that step, so a crash during the paid call loses it. One way around it is to write the delegation as an intent record to an external store before the call (status pending, with your own job id), then update it with the provider’s reference afterwards. Where the provider accepts an idempotency key or metadata, pass that job id through so a charge with no saved delegation can still be matched back. Anything on a bill that matches nothing becomes its own exception type, same as amount_mismatch.

When I built multi-agent systems for clients we did this kind of reconciliation with spreadsheets and shell scripts, and the audit trail was always the part that fell apart. How are you handling providers whose bills land after the reconcile node has already run?