Skip to content

How to dynamically add/remove tools in a tool_use_behavior="run_llm_again"-loop #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Afstkla opened this issue May 27, 2025 · 3 comments
Labels
question Question about using the SDK

Comments

@Afstkla
Copy link

Afstkla commented May 27, 2025

Please read this first

  • Have you read the docs?Agents SDK docs: yes :)
  • Have you searched for related issues? Others may have had similar requests: also yes :)

Question

I'm building an agent that's often running multiple tool calls sequentially, and sometimes tool A will 'unlock' tools B, C, and D. But I so far haven't found a way to 'inject' those during an automated run_llm_again loop. And I'm really dreading writing all the logic required to manually handle the run loop.

I've attempted using the Hooks to dynamically add new tools, I've been trying to work with various different Agents as tools, where the tools of those Agents are added dynamically through the hooks. The only thing that's semi worked for me is to use the handoff feature, but then I can't give control back to the 'main' agent.

My agent's flow will normally be like:

  1. User asks for information from Gmail
  2. Agent Runner starts
  3. Agent detects that there's no Gmail connection set up
  4. Agent will call the "initiate gmail connection" tool
  5. Response from the tool 'unlocks' all Gmail actions
  6. Agent calls the 'get latest emails' tool
  7. Agent streams the response
  8. Agent Runner exits

But this is a workflow that I can't currently figure out. Is there a way?

@Afstkla Afstkla added the question Question about using the SDK label May 27, 2025
@rm-openai
Copy link
Collaborator

This is a good question. I'll have to think of a long term good answer, but the short term hack is: just add the tools in the initiate_gmail_connection tool call.

my_agent = Agent(instructions=[...])

@function_tool
def initiate_gmail_connection():
   my_agent.tools.extend([send_gmail, read_gmail, ...])

@Afstkla
Copy link
Author

Afstkla commented May 28, 2025

Thanks @rm-openai , unfortunately, it seems that extending the tools isn't enough reason to refresh the current_span / agent_span in the _run_streamed_impl (

if current_span is None:
), and therefore extending the tools doesn't work within the run_llm_again loop (or I tested it wrong, ofc also possible).

Any other ideas?

@bakikucukcakiroglu
Copy link

@Afstkla I have a system with dynamic agents. I mean users define their agents by entering instructions, settings handoffs, and assigning tools from a predefined set of tools. I fetch the agents config of the user when a request arrives to my backend and dynamically create agents and run them. This system works well so far.

The problem occurs where more than one tool need to be executed in order. Let me give me an example. Let's say we have two tools one for creating a discount code (create_discount) and one for sending an email (send_email). If they want to show it in the chat, they can only add create_dicount tool to the agent and write the prompt accordingly. This scenario works perfectly in my system. However, if the user wants to send the discount code to the customer's email then they add both tool to the agent and explain the flow in the prompt. In this scenario, the system only calls create_dicount but not send_email. I would expect from an agent to consider all tools again (like it does in the initial call) when it is called again with the tool response.

Is the any official way of doing what I desire? Or should we workaround this manually? Also if there is no official way, I think there should be because then developers can create strong atomic tools and users may achieve an infinite amount of different things using a this small set if tools. The current system don't allow this. What do you think @rm-openai?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about using the SDK
Projects
None yet
Development

No branches or pull requests

3 participants