Skip to content

How to ensure a specific tool sequencing? #918

Closed

Description

@maininformer

I would like to give varying degrees of control to my agents. For example I would like a certain tool to not be available until another tool is called. Or, similarly, I don't want the agents to be able to handoff without calling a tool first.

Is there a way around this without something like this where we return from an agent?

I tried something like this:

class PlannerHooks(AgentHooks):
    def __init__(self):
        self._saved_handoffs: List[Handoff] | None = None

    async def on_start(self, ctx, agent):
        # remove the handoff
        if self._saved_handoffs is None:
            self._saved_handoffs = list(agent.handoffs)
        agent.handoffs = []  # special line
        agent.tools = [t for t in agent.tools if t.name != "transfer_to_executor"]

    async def on_tool_end(self, ctx, agent, tool, result):
        if tool.name != "specific_tool":
            return

        agent.handoffs = self._saved_handoffs
        agent.tools.append(FunctionTool(name="transfer_to_executor", description="Transfer to the executor agent.", function=lambda x: x))

        agent.model_settings = ModelSettings(tool_choice="transfer_to_executor")
        agent.reset_tool_choice = False

    async def on_handoff(self, context: RunContextWrapper, agent: Agent, source: Agent) -> None:
        agent.model_settings = ModelSettings(tool_choice=STARTING_TOOL_CHOICE[agent.name])
        agent.reset_tool_choice = False

This used to work but it no longer does. In particular, this code used to work without the special line above; then it it broke, meaning the agent would attempt to call the tool I removed, the handoff. I then added the special line and this gets me an OpenAI server error with a request ID. So it seems that changes made to tools during on_start do not take effect.

Activity

  1. added
    question
    on Jun 23, 2025
  2. rm-openai commented on Jun 23, 2025

    @rm-openai
    Collaborator

    Yes you can do something like this:

    class MyContext:
      should_enable_foo_tool: bool = False
    
    def is_foo_enabled(ctx, agent) -> bool:
      return ctx.context.should_enable_foo_tool
    
    @function_tool(is_enabled=is_foo_enabled)
    def foo_tool(...):
      ...
    
  3. maininformer commented on Jun 23, 2025

    @maininformer
    Author

    Thank you; and how would I do this on a handoff? that is an implicitly defined tool. Should I set its is_enabled during lifecycle calls or ...?

  4. rm-openai commented on Jun 23, 2025

    @rm-openai
    Collaborator

    Ah. Let me add it to handoffs too.

  5. added a commit that references this issue on Jun 24, 2025
  6. added a commit that references this issue on Jun 24, 2025
  7. added
    enhancement
    and removed
    question
    on Jun 25, 2025
  8. seratch commented on Jun 25, 2025

    @seratch
    Member

    Fixed by #925

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeature:core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions