Skip to content

MemoryStream passed to as_tool() does not capture sub-agent events #2888

@ohdearquant

Description

@ohdearquant

Summary

When using agent.as_tool(stream=MemoryStream()) to wrap a sub-agent as a tool, the MemoryStream passed via the stream parameter does not capture the sub-agent's events. After the run completes, await stream.history.get_events() returns an empty list for the sub-agent streams, while the parent agent's stream correctly records all events (including the ToolCallEvents for the sub-agent invocations).

Reproduction

See notebooks/04_events.ipynb — the tutorial creates separate MemoryStream instances for a surveyor and critic sub-agent:

surveyor_stream = MemoryStream()
critic_stream = MemoryStream()

surveyor = Agent("surveyor", config=config, tools=[exa], prompt="...")
critic = Agent("critic", config=config, prompt="...")

researcher = Agent(
    "researcher",
    config=config,
    tools=[
        exa,
        surveyor.as_tool(description="Survey literature", stream=surveyor_stream),
        critic.as_tool(description="Evaluate a hypothesis", stream=critic_stream),
    ],
    ...
)

reply = await researcher.ask("...", stream=research_stream)

# Parent stream has events ✓
# surveyor_stream and critic_stream are empty ✗

Question

Is this the intended behavior? The stream parameter on as_tool() suggests it should capture the sub-agent's events, but it may serve a different purpose (e.g., persistent_stream for cross-call history reuse rather than event capture). If this is by design, it would help to clarify in the docs what the stream parameter does vs. how to actually observe sub-agent events.

Metadata

Metadata

Assignees

No one assigned

    Labels

    betabugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions