Add `@agent.on_event` for registering event listeners on an `Agent`
Observing one capability event from application code currently costs a Hooks instance, a registration on it, and an extra entry in capabilities=:
hooks = Hooks()
@hooks.on.event(SpendRecordedEvent)
async def show(ctx, event):
print(event.usd)
agent = Agent('anthropic:claude-fable-5', capabilities=[SpendLimits(...), hooks])
Four lines to watch one event, and the Hooks object is pure ceremony when events are all you want. That cost lands exactly as capabilities move their bespoke callbacks over to events (pydantic/pydantic-ai-harness#714 deprecates five), turning a one-line on_spend= kwarg into a block.
This adds the shorthand:
agent = Agent('anthropic:claude-fable-5', capabilities=[SpendLimits(...)])
@agent.on_event(SpendRecordedEvent)
async def show(ctx, event):
print(event.usd)
Bare works too — @agent.on_event on its own sees every AgentStreamEvent, with the argument typed as the full union.
Agent already registers tools, instructions, system prompts and output validators by decorator, so this joins that family rather than starting one. It delegates to a Hooks instance the agent owns, so filtering, typing, timeout= and dispatch keep a single implementation.
Why only events
The obvious generalisation is an agent.hooks property exposing every family. The other families are interceptors: they sit in a wrap chain, take and return the value, and their position relative to the other capabilities is load-bearing. An implicit instance has to pick that position for you, and whichever rule is chosen, the first person who needs it elsewhere has to abandon agent.hooks and go back to an explicit instance — a cliff exactly where the convenience was meant to help. A listener returns nothing and has no chain position to get wrong.
This does not foreclose agent.hooks later, with on_event remaining the shorthand the way @agent.tool is shorthand for a toolset.
Behaviour
- Runs after the capabilities' own listeners, so an agent-level listener observes what the capabilities did.
- Survives an overridden root capability. The instance is held outside
_root_capability, mirroring the agent's function toolset being appended outside theoverride_toolsetsbranch so@agent.tooltools surviveoverride(toolsets=...). - Can emit a
CustomEvent, being application code — the supported way to republish a capability's internal event to a frontend. - Costs nothing unused. With no listeners the
Hooksis never wrapped into the run at all, and an empty one reportslistens_to()asFalseregardless, so the dispatch gate would skip it anyway.
Linked Issue
Fixes #8100
Checklist
- Linked issue exists and is referenced above
- Tests added/updated for new behavior
- Documentation updated
-
make lint && make typecheck && make testpasses locally (see below)
Verification
Four tests in tests/test_on_event.py covering filtering, the bare form, ordering against a capability's own listener, CustomEvent emission from an agent listener, and that nothing is wrapped in until a listener exists. tests/test_on_event.py 40 passed; test_on_event + test_capability_events + test_custom_events + test_capability_hooks + test_agent 761 passed; hooks.md examples 12 passed.
ruff check and ruff format clean across 749 files. pyright on agent/__init__.py reports 0 errors. A full pyright run reports 62 errors here, all in embeddings/sentence_transformers.py, providers/voyageai.py and their tests: both extras are gated python_version < '3.14' and this checkout runs 3.14.3, so the imports do not resolve locally. A baseline run on an unmodified main checkout in the same environment reports the same 62.