Use OpenAI Decisions API to Choose Tools for Your Agent
Job to be done: Select the correct tool or model for an agent using OpenAI Decisions API.
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
Automate selecting the best Python library for a coding assignment based on its description, saving time on research.
- 9-5 employee
Help your AI assistant pick the right API endpoint or software tool for a repetitive task, reducing errors and costs.
- Entrepreneur
Build an AI agent that intelligently chooses between different marketing tools (e.g., email, social media) for campaign tasks.
What you’ll get
You’ll learn how to use the OpenAI Decisions API to help your AI agent pick the right tool or model for a specific task. This approach is useful because it makes your agent more efficient by ensuring it uses the most appropriate and cost-effective tool for each request, rather than guessing or using a powerful, expensive model for simple tasks.
Tools you need
- OpenAI Decisions API (paid): A specialized API from OpenAI that helps AI agents make specific choices from a predefined list, returning a selection with a confidence score.
- Strands Agents (free): An open-source framework for building AI agents, which can be customized with different tools and models.
- AsyncOpenAI (free): The official asynchronous Python client library for interacting with OpenAI’s APIs, including the Decisions API.
Steps
-
Set up your environment: Ensure you have Python installed and install the necessary libraries. You will also need an OpenAI API key. The author uses
gpt-6-lunaas a model, but you may need to use a different available model. The exact setup for Strands Agents andAsyncOpenAIis not fully detailed, but typically involves installing them via pip and setting your API key as an environment variable.pip install openai strands-agents export OPENAI_API_KEY='your-api-key-here'You should see the libraries installed without errors.
-
Define your tools and the decision prompt: The author uses a list of tools (
ALL_TOOLS) and a system prompt (PROMPT) for the agent. For the Decisions API, you need to define the choices the agent can make, each with a value and a description. The author provides an example of how to format these choices for selecting a tool.# Example of how choices might be formatted for the Decisions API oai_choices = [ { "value": t.tool_name, "description": t.tool_spec["description"] } for t in ALL_TOOLS ]You should have a list of dictionaries, where each dictionary represents a possible choice for the agent.
-
Create a function to call the Decisions API: This function will take a user’s query and use the
AsyncOpenAIclient to call the Decisions API. It will pass the query, the chosen model, and the list of possible choices. The API will return the agent’s decision.from openai import AsyncOpenAI oai = AsyncOpenAI() async def openai_select(query): resp = await oai.decisions.create( model="gpt-6-luna", # Or another available model input=query, questions=[ { "type": "choice", "name": "tool", "instructions": "Which tool answers this request?", "choices": oai_choices # This list comes from step 2 } ], ) answer = next(a for a in resp.answers if a.name == "tool") # The author returns a list containing the selected tool, # but the exact format depends on how Strands Agents expects it. # For example, it might be: return [BY_NAME[answer.choice]] # where BY_NAME maps the choice value back to a tool object. return answer.choice # Simplified for demonstrationYou should be able to run this function without errors, and it should return a string representing the chosen tool’s name or value.
-
Integrate the decision function into your agent: The author shows how to use this function within a Strands Agent by using a
ToolFilterHook. This hook intercepts the agent’s process before it calls a model, allowing you to filter or select the tools the agent can use based on the Decisions API’s output.# Assuming you have defined ALL_TOOLS, AGENT_MODEL, PROMPT, and BY_NAME # and the openai_select function from step 3 # Example hook definition (details may vary based on Strands Agents library) class ToolFilterHook: def __init__(self, selector_func): self.selector_func = selector_func async def __call__(self, event): if event.name == "BeforeModelCallEvent": selected_tool_name = await self.selector_func(event.query) # Filter event.tools to only include the selected tool event.tools = [t for t in event.tools if t.tool_name == selected_tool_name] # Instantiate the agent with the hook agent = Agent( model=AGENT_MODEL, tools=ALL_TOOLS, system_prompt=PROMPT, hooks=[ToolFilterHook(openai_select)], callback_handler=None ) # Invoke the agent with a query result = await agent.invoke_async("How much is a room at the Cliffside Resort?")When you invoke the agent, it should process the query, use the Decisions API to select the most relevant tool (e.g.,
get_hotel_room_rates), and then proceed with that tool. The output should indicate which tool was chosen and potentially the confidence score.
Original source
This workflow is based on a blog post by elizabethfuentes12 on DEV Community. The author explains how to integrate OpenAI’s Decisions API with Strands Agents to create more efficient AI agents by having them intelligently select the right tools for specific tasks.
Notes & variations
- Free-tier alternative: While the OpenAI Decisions API itself is a paid service, you can explore using open-source models or simpler logic within Strands Agents if cost is a major concern. However, for the specific functionality of the Decisions API, there isn’t a direct free alternative.
- Common mistake: A common pitfall is not correctly formatting the
choicesparameter for the Decisions API. Each choice needs avalue(the identifier for the choice) and adescription(explaining what the choice does). Ensure these are clear and distinct. - Tip for better results: Provide very clear and concise instructions in the
instructionsfield of thequestionsparameter for the Decisions API. The more specific your instructions, the better the API can understand the task and make an accurate choice.