Skip to content

Tool deferrals with Agent delegation #3527

@geodavic

Description

@geodavic

Question

What is the suggested way to handle tool deferrals for agents that have been delegated a task by another agent? For example, in the agent delegation section of the docs, there's an example where the joke selection agent can delegate the task of generating a joke to the joke generation agent. What if the joke generation agent has a deferred tool response?

In other words: agent A has a tool that calls agent B, and agent B has a tool that requires deferral. What is the right way to forward the DeferredToolRequests from agent B all the way back to the user? And then how do you resume agent B's execution with the resulting DeferredToolResults?

Here is a minimal example:

from pydantic_ai import Agent, RunContext

joke_selection_agent = Agent(  
    'gateway/openai:gpt-5',
    system_prompt=(
        'Use the `joke_factory` to generate some jokes according to the user's preference, then choose the best. '
        'You must return just a single joke.'
    ),
)
joke_generation_agent = Agent(  
    'gateway/google-gla:gemini-2.5-flash', output_type=list[str]
)

@joke_generation_agent.tool(requires_approval=True)
def get_news_headlines(date: str) -> list[str]:
    ...

@joke_selection_agent.tool
async def joke_factory(ctx: RunContext[None], count: int, preference: str) -> list[str]:
    r = await joke_generation_agent.run(  
        f'Please generate {count} jokes about {preference}.',
    )
    return r.output  


result = joke_selection_agent.run_sync(
    'Tell me a joke about current events.',
    usage_limits=UsageLimits(request_limit=5, total_tokens_limit=500),
)

Since the prompt was asking to generate a joke about current events, the joke selection agent will call the joke generation agent, which will call the get_news_headlines() tool because it will need to know what the current events are. But that tool requires approval. How would one handle the DeferredToolRequests from the joke generation agent?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions