feat(agents): opt-in agent lifecycle events (start/finish) in the stream#6269
Open
ferponse wants to merge 1 commit into
Open
feat(agents): opt-in agent lifecycle events (start/finish) in the stream#6269ferponse wants to merge 1 commit into
ferponse wants to merge 1 commit into
Conversation
Add RunConfig.emit_agent_lifecycle_events (default False). When enabled, each agent invocation yields a lightweight lifecycle marker event before its logic and after it, authored by the agent and carrying Event.custom_metadata['agent_lifecycle'] == 'start' | 'finish' (plus the agent's branch). This lets consumers bracket agent/node execution exactly — including per LoopAgent iteration and parallel-branch boundaries — without inferring boundaries from author/branch transitions. Additive and default-off: existing event streams are unchanged. No Event schema change (it reuses the existing custom_metadata bucket). Fixes google#6267.
This was referenced Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The
runner.run_asyncstream carries text / tool-call / state / reasoning events, but no explicit "agent X started / finished" boundaries. Consumers that want to bracket work per node/agent (workflows, multi-agent, loops) must infer boundaries fromauthor/branchtransitions, which is lossy — e.g. exact parallel-branch end can't be observed, and aParallelAgentinside aLoopAgentcan't be split per iteration (its events are identical across iterations, see #6266).This adds an opt-in
RunConfig.emit_agent_lifecycle_events(defaultFalse). When enabled, every agent invocation yields a lightweight lifecycle marker event before its logic and after it — authored by the agent, carryingEvent.custom_metadata['agent_lifecycle'] == 'start' | 'finish'(plus the agent'sbranch).Fixes #6267.
Why
We're building the AG-UI ↔ ADK middleware (
ag-ui-adk), which maps ADK workflow nodes to AG-UISTEP_STARTED/STEP_FINISHEDevents. Explicit lifecycle events let any consumer bracket nodes exactly, matching what other agent frameworks already emit (CrewAIMethodExecutionStarted/Finished, AWS Strandsmultiagent_node_start/stop).How
RunConfig.emit_agent_lifecycle_events: bool = False.BaseAgent.run_asyncemits astartmarker once the agent is actually going to run (i.e. not short-circuited bybefore_agent_callback) and a pairedfinishmarker on every exit path.Event.custom_metadatabucket — noEventschema change — and propagate through the tree (run_configis copied into each sub-agent context), so they fire per agent, including perLoopAgentiteration.Default-off: existing event streams are byte-for-byte unchanged.
Example
LoopAgent(max_iterations=2, sub_agents=[ParallelAgent(sub_agents=[p, q])])withemit_agent_lifecycle_events=True:Every node gets a balanced start/finish per invocation, so iterations and parallel branches are exactly bracketed.
Tests
tests/unittests/agents/test_base_agent.py: addedtest_run_async_emits_agent_lifecycle_events(start → content → finish) andtest_no_agent_lifecycle_events_by_default(backward-compat: stream unchanged). The agents test suite (test_base_agent,test_loop_agent,test_sequential_agent,test_parallel_agent,test_run_config) passes.Notes
custom_metadata) per maintainer preference.