ó
    °"³j  ã                  óž   • % S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
Jr  Sr\S	   " S
SS9rS\S'   \SS j5       rSS jrSS jrSS jrg)u  Record run usage, attributing it to the agent run that produced it.

An agent run span reports the usage of the requests *that run* made â€” not its nested runs', which
report their own. Summing the agent-run spans in a trace then gives the conversation's total,
matching the sum of the `chat` spans underneath them, which is the point of keeping agent-run usage
in its own attribute namespace: a backend can add these up without counting anything twice.

`RunUsage` alone can't say who produced what. It is accumulated into in place, and the multi-agent
delegation pattern hands the *same* object to concurrent delegates (`usage=ctx.usage`), so neither
the object's contents nor an end-minus-start delta on it distinguishes this run's requests from a
sibling's â€” concurrent delegates absorb each other.

Which run is producing is a property of the call stack, so that is what this follows. [`accumulate`]
[] makes a run's `RunUsage` the one credited for as long as its span is open, and restores the
enclosing run's on the way out; the `record_*` functions credit only that innermost run. Asyncio
copies the context when a task is created, so concurrent delegates each start from the parent's and
replace it with their own, never seeing each other's.

This module owns the *only* in-place mutation of a run's usage. Incrementing a `RunUsage` field
directly leaves its tokens off the run's span, so `tests/test_usage_attribution.py` fails on a bare
`requests += `, `tool_calls += `, or `.incr(` that isn't marked `# usage-attribution: ok` with a
reason.
é    )Úannotations)Ú	Generator)Úcontextmanager)Ú
ContextVaré   )ÚRequestUsageÚRunUsage)Ú
accumulateÚrecord_requestÚrecord_tool_callÚrecord_usagezRunUsage | Nonezpydantic_ai.usage_attributionN)ÚdefaultzContextVar[RunUsage | None]Ú_activec              #  óž   #   • [         R                  U 5      n Sv •  [         R                  U5        g! [         R                  U5        f = f7f)zúCredit `run_usage` with what is recorded in this context, until the block exits.

A nested run replaces it for the length of its own span, so what the nested run records is its
own; resetting on the way out hands crediting back to the enclosing run.
N)r   ÚsetÚreset)Ú	run_usageÚtokens     Ú[/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/_usage_attribution.pyr
   r
   &   s5   é € ô �K‰K˜	Ó"€EðÛä�‰�eÕøŒ�‰�eÕüs   ‚A™3 �A³A
Á
Ac                óˆ   • U =R                   S-  sl         [        R                  5       =nb  U=R                   S-  sl         gg)zRCount one model request against this run's usage and against the run that made it.r   N)Úrequestsr   Úget©Úusager   s     r   r   r   4   s7   € à	‡N‚N�aÑ…NÜ—[‘[“]Ð"ˆ	Ñ/Ø×Ò˜aÑÖð 0ó    c                óˆ   • U =R                   S-  sl         [        R                  5       =nb  U=R                   S-  sl         gg)zYCount one successful tool call against this run's usage and against the run that made it.r   N)Ú
tool_callsr   r   r   s     r   r   r   ;   s:   € à	×Ò˜ÑÕÜ—[‘[“]Ð"ˆ	Ñ/Ø×Ò Ñ!Öð 0r   c                óx   • U R                  U5        [        R                  5       =nb  UR                  U5        gg)uõ   Add recorded usage to this run's usage and to the run that produced it.

`recorded` is one response's `RequestUsage`, or the `RunUsage` delta a durable operation
accumulated across the boundary â€” which carries its own requests and tool calls.
N)Úincrr   r   )r   Úrecordedr   s      r   r   r   B   s2   € ð 
‡J�JˆxÔÜ—[‘[“]Ð"ˆ	Ñ/Ø�‰�xÕ ð 0r   )r   r	   ÚreturnzGenerator[None])r   r	   r!   ÚNone)r   r	   r    zRunUsage | RequestUsager!   r"   )Ú__doc__Ú
__future__r   Úcollections.abcr   Ú
contextlibr   Úcontextvarsr   r   r   r	   Ú__all__r   Ú__annotations__r
   r   r   r   © r   r   Ú<module>r+      s]   ðòõ0 #å %Ý %Ý "ç )à
N€à'1Ð2CÒ'DÐEdÐnrÑ's€Ð	$Ó sð ó
ó ð
ô ô"õ!r   