lpgcast.llm.usage¶
Token and call accounting.
Tokens a provider billed are kept apart from tokens replayed out of the response cache, so a cached rerun never reads as spend and a cost column still says what a fresh run would cost.
Classes¶
TokenUsage
¶
Bases: BaseModel
Token counts for one call, as far as the provider reports them.
None means not reported, which is not zero.
Source code in lpgcast/llm/usage.py
Attributes¶
cache_read_input_tokens = Field(default=None, description="Prompt tokens served from the provider's prompt cache, counted inside input_tokens. Unrelated to LPGCast's response cache.")
class-attribute
instance-attribute
¶
input_tokens = Field(default=None, description='Prompt tokens.')
class-attribute
instance-attribute
¶
output_tokens = Field(default=None, description='Completion tokens.')
class-attribute
instance-attribute
¶
reasoning_tokens = Field(default=None, description='Thinking tokens, counted inside output_tokens.')
class-attribute
instance-attribute
¶
Methods:¶
__add__(other)
¶
Source code in lpgcast/llm/usage.py
UsageTracker
¶
Bases: BaseModel
Calls, events and tokens for a run or a scope within it.
Source code in lpgcast/llm/usage.py
Attributes¶
billed = Field(default_factory=TokenUsage, description='Tokens from live provider calls.')
class-attribute
instance-attribute
¶
cache_hits = Field(default=0, description='Responses served from cache.')
class-attribute
instance-attribute
¶
calls = Field(default=0, description='Provider calls that returned.')
class-attribute
instance-attribute
¶
calls_failed = Field(default=0, description='Provider calls that raised.')
class-attribute
instance-attribute
¶
chars_received = Field(default=0, description='Response characters.')
class-attribute
instance-attribute
¶
chars_sent = Field(default=0, description='Prompt characters sent.')
class-attribute
instance-attribute
¶
json_repairs = Field(default=0, description='Bracket-kind repairs.')
class-attribute
instance-attribute
¶
json_schema_fallbacks = Field(default=0, description='Switches from json_schema to json_object.')
class-attribute
instance-attribute
¶
parse_abandoned = Field(default=0, description='Requests given up on.')
class-attribute
instance-attribute
¶
parse_retries = Field(default=0, description='Re-asks after bad JSON.')
class-attribute
instance-attribute
¶
rate_limited = Field(default=0, description='Subset of calls_failed.')
class-attribute
instance-attribute
¶
refused = Field(default=0, description='Replies the model refused.')
class-attribute
instance-attribute
¶
rejected = Field(default=0, description='Subset of calls_failed: configuration faults.')
class-attribute
instance-attribute
¶
replayed = Field(default_factory=TokenUsage, description='Tokens of responses served from cache.')
class-attribute
instance-attribute
¶
timeouts = Field(default=0, description='Subset of calls_failed.')
class-attribute
instance-attribute
¶
truncated = Field(default=0, description='Replies cut off at the output token limit.')
class-attribute
instance-attribute
¶
Methods:¶
add_call(chars_sent, chars_received, usage)
¶
Charge one live provider call.
add_hit(usage)
¶
Functions:¶
active_tracker()
¶
token_usage_from_openai(usage)
¶
Parse an OpenAI-shaped usage object, an SDK model or a dict.
Source code in lpgcast/llm/usage.py
track_usage(tracker=None)
¶
Charge calls made in this block (and tasks it spawns) to tracker too.
Task-local: concurrent episodes each charge their own tracker.