Skip to content

API Reference parrot.core.events.lifecycle.events.client

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.core.events.lifecycle.events.client

View source

LLM Client lifecycle events.

FEAT-176 — Lifecycle Events System.

Covers: before/after/failed LLM API calls and per-chunk streaming events.

Note: ClientStreamChunkEvent is high-frequency and NEVER dual-emits to EventBus by default. It must be explicitly opted in via forward_to_bus=True on the subscription.

Class AfterClientCallEvent

Emitted after a successful LLM API call completes.

NOT emitted when the call fails (ClientCallFailedEvent is used instead).

Parameter Type Default Description
client_name str ''
model str ''
duration_ms float 0.0
input_tokens Optional[int] None
output_tokens Optional[int] None
finish_reason Optional[str] None
agent_name Optional[str] None
user_id Optional[str] None
session_id Optional[str] None
tenant Optional[str] None
cost_usd Optional[float] None
attribution Optional[UsageAttribution] _snapshot_attribution()
cache_read_tokens Optional[int] None
cache_creation_tokens Optional[int] None
reasoning_output_tokens Optional[int] None
source_cost_usd Optional[float] None
price_revision Optional[str] None
price_valid_from Optional[str] None
pricing_policy Optional[str] None

Fields:

Field Type Default Description
client_name str '' Provider identifier.
model str '' Model name/identifier.
duration_ms float 0.0 Wall-clock time in milliseconds.
input_tokens Optional[int] None Input token count (provider-dependent; may be None).
output_tokens Optional[int] None Output token count (provider-dependent; may be None).
finish_reason Optional[str] None Stop reason returned by the provider (e.g., "stop", "max_tokens"). May be None.
agent_name Optional[str] None AbstractBot.name of the invoking agent, or None when called outside a bot invocation scope. Set by the client from the current_agent_name ContextVar (FEAT-228).
user_id Optional[str] None Optional user identifier for per-user usage attribution. Set from the current_user_id ContextVar. Included in span attributes only — NEVER in metric labels.
session_id Optional[str] None Optional session identifier. Set from the current_session_id ContextVar. Spans only.
tenant Optional[str] None Optional server-resolved tenant identifier.
cost_usd Optional[float] None Optional call cost in USD. None when pricing is unknown.
attribution Optional[UsageAttribution] field(default_factory=_snapshot_attribution) Immutable SDD attribution snapshot from construction time.
cache_read_tokens Optional[int] None Optional prompt-cache tokens read from the provider.
cache_creation_tokens Optional[int] None Optional prompt-cache tokens created by the provider.
reasoning_output_tokens Optional[int] None Optional reasoning tokens reported by the provider.
source_cost_usd Optional[float] None Provider-reported cost retained for reconciliation only.
price_revision Optional[str] None
price_valid_from Optional[str] None
pricing_policy Optional[str] None Derived seat policy; None is equivalent to "billed".

Class BeforeClientCallEvent

Emitted just before an LLM API call is made.

Parameter Type Default Description
client_name str ''
model str ''
temperature Optional[float] None
system_prompt_hash str ''
has_tools bool False
agent_name Optional[str] None
user_id Optional[str] None
session_id Optional[str] None
tenant Optional[str] None

Fields:

Field Type Default Description
client_name str '' Provider identifier ("anthropic", "openai", etc.).
model str '' Model name/identifier being called.
temperature Optional[float] None Sampling temperature (None if not configured).
system_prompt_hash str '' SHA-256 hex of the system prompt. NEVER the prompt itself — this preserves privacy while enabling correlation.
has_tools bool False True if tool definitions were included in the request.
agent_name Optional[str] None AbstractBot.name of the invoking agent, or None when called outside a bot invocation scope. Set by the client from the current_agent_name ContextVar (FEAT-228). NEVER contains PII (user_id, session_id, prompt content).
user_id Optional[str] None
session_id Optional[str] None
tenant Optional[str] None Optional server-resolved tenant identifier.

Class ClientCallFailedEvent

Emitted when an LLM API call raises an exception.

AfterClientCallEvent is NOT emitted when this fires.

Parameter Type Default Description
client_name str ''
model str ''
duration_ms float 0.0
error_type str ''
error_message str ''
agent_name Optional[str] None
user_id Optional[str] None
session_id Optional[str] None
tenant Optional[str] None
attribution Optional[UsageAttribution] _snapshot_attribution()

Fields:

Field Type Default Description
client_name str '' Provider identifier.
model str '' Model name/identifier.
duration_ms float 0.0 Wall-clock time in milliseconds until failure.
error_type str '' type(exc).__name__ of the exception.
error_message str '' String representation of the exception.
agent_name Optional[str] None AbstractBot.name of the invoking agent, or None when called outside a bot invocation scope. Set by the client from the current_agent_name ContextVar (FEAT-228).
user_id Optional[str] None Optional user identifier for per-user usage attribution. Spans only — NEVER in metric labels.
session_id Optional[str] None Optional session identifier. Spans only.
tenant Optional[str] None Optional server-resolved tenant identifier.
attribution Optional[UsageAttribution] field(default_factory=_snapshot_attribution) Immutable SDD attribution snapshot from construction time.

Class ClientRoundEvent

Emitted after each tool-execution round inside a client's ask() loop.

FEAT-397 — Per-Round Token Usage Observability.

NOT emitted for single-round calls (no tool use). Token fields are None when the provider did not report usage for the round (e.g. some streaming paths) — the round's accumulator skips None usage rather than corrupting the running total.

Parameter Type Default Description
client_name str ''
model str ''
round_number int 0
input_tokens Optional[int] None
output_tokens Optional[int] None
total_tokens Optional[int] None
tool_calls tuple ()
duration_ms float 0.0
raw_usage Optional[dict] None
agent_name Optional[str] None
user_id Optional[str] None
session_id Optional[str] None
tenant Optional[str] None

Fields:

Field Type Default Description
client_name str '' Provider identifier ("anthropic", "openai", etc.).
model str '' Model name/identifier being called.
round_number int 0 1-indexed round number within this ask() call.
input_tokens Optional[int] None This round's input token count, or None if the provider reported no usage for the round.
output_tokens Optional[int] None This round's output token count, or None.
total_tokens Optional[int] None This round's total token count, or None.
tool_calls tuple () Tool-name strings invoked during this round. Uses tuple for immutability; to_dict() converts to list.
duration_ms float 0.0 Wall-clock time in milliseconds for this round's SDK call.
raw_usage Optional[dict] None Provider-native usage payload for this round, JSON-safe (plain dict — never a nested Pydantic model, never PII/prompt content).
agent_name Optional[str] None AbstractBot.name of the invoking agent, or None when called outside a bot invocation scope. Set by the client from the current_agent_name ContextVar (FEAT-228).
user_id Optional[str] None Optional user identifier for per-user usage attribution. Set from the current_user_id ContextVar. Included in span attributes only — NEVER in metric labels.
session_id Optional[str] None Optional session identifier. Set from the current_session_id ContextVar. Spans only.
tenant Optional[str] None Optional server-resolved tenant identifier.

Class ClientStreamChunkEvent

Emitted for each chunk received during a streaming response.

HIGH-FREQUENCY event. This event NEVER dual-emits to EventBus by default, even if a subscription has forward_to_bus=True — subscribers must explicitly request bus forwarding.

Contains chunk metadata only (index + size), NEVER the chunk text, to avoid PII leakage and keep per-chunk overhead minimal.

Parameter Type Default Description
client_name str ''
model str ''
chunk_index int 0
chunk_size_bytes int 0

Fields:

Field Type Default Description
client_name str '' Provider identifier.
model str '' Model name/identifier.
chunk_index int 0 Zero-based index of this chunk in the stream.
chunk_size_bytes int 0 UTF-8 encoded byte length of this chunk.

Class PromptCacheAppliedEvent

Emitted when prompt caching is applied to an LLM call.

FEAT-181 — Provider-Agnostic Prompt Caching.

Parameter Type Default Description
client_name str ''
model str ''
blocks_marked int 0
est_tokens int 0
segment_hashes tuple ()

Fields:

Field Type Default Description
client_name str '' Provider identifier ("anthropic", "openai", etc.).
model str '' Model name/identifier being called.
blocks_marked int 0 Number of cache_control blocks applied to the system prompt. For Anthropic: number of cacheable blocks (≤4). For OpenAI/Gemini: 0 (caching is implicit or resource-based).
est_tokens int 0 Estimated cacheable token count (rough 4-chars-per-token estimate). Used for observability only.
segment_hashes tuple () SHA-256 hashes of each cacheable segment text. NEVER the raw segment content — privacy-safe. Uses tuple for immutability; to_dict() converts to list.

Class PromptCacheSkippedEvent

Emitted when prompt caching is skipped.

FEAT-181 — Provider-Agnostic Prompt Caching.

Parameter Type Default Description
client_name str ''
model str ''
reason Literal['below_threshold', 'provider_unsupported', 'feature_off', 'no_segments', ''] ''

Fields:

Field Type Default Description
client_name str '' Provider identifier.
model str '' Model name/identifier.
reason Literal['below_threshold', 'provider_unsupported', 'feature_off', 'no_segments', ''] '' Why caching was skipped. One of: - "below_threshold" — cacheable token count < _min_cache_tokens. - "provider_unsupported" — provider does not support caching. - "feature_off" — prompt_caching=False at the bot level. - "no_segments" — no segments were passed to _apply_cache_hints.

Alias UsageAttribution

Re-exported from parrot.observability.attribution.UsageAttribution.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally