Using OpenAI Tools to Build Agentic AI.ai

Using OpenAI Tools to Build Agentic AI

OpenAI recently released a suite of tools designed to help developers and enterprises build and deploy their own agentic AI. For those new to agentic AI, it’s when AI can act on its own with very little human interaction or guidance.

OpenAI’s release is a significant boost for advanced AI applications, as complex reasoning, multimodal interactions, and multi-step tasks are some of the most challenging AI activities to implement, even for advanced AI developers and machine learning experts. Without sufficient training or extensive human oversight, the usefulness of AI solutions like large language models (LLMs) is highly limited.

To remedy these shortcomings, OpenAI recently released a suite of tools designed to make developing your own agentic AI as easy as possible. To help you build agentic AI using OpenAI tools, we’ve put together a guide to make the process as painless as possible.

How To Build Agentic AI Using OpenAI Tools

OpenAI’s tools for agentic AI have three main components. There’s the Responses API, which elaborates upon the earlier Chat Completions API. Then there are tools for searching the web or local computers. Finally, the new Agents SDK makes it fast and easy to implement single-agent and multi-agent workflows in various programming languages. There’s even a suite of observability tools that allows you to monitor how the agentic AI performs.

To help you get started using OpenAI tools to build agentic AI, let’s delve into each of these components to help give you a better idea of how to use them in your projects. We’ll show you how to use the new Responses API to create a book recommendation AI that can be trained on local data and then customized for different applications.

Responses API

The new Responses API is an extension of the previous Chat Completion API, the basic model used by LLMs like ChatGPT.

The Responses API adds the ability to look things up on the web or search for files on local systems. It’s intended for developers who want their models to access the real world without the hassle of stringing multiple APIs together. It improves the Chat Completion API in various ways, making it easier to look up a particular item, integrate streaming events, or format the model’s output.

Chat Completion API also makes storing data easier for an LLM, which isn’t something OpenAI normally does. This is ideal for developers who want to train their models using their own data, another area where OpenAI traditionally falls short.

Web Search With OpenAI Tools

The ability for an AI agent to interact with the internet eliminates many of the shortcomings LLMs are sometimes susceptible to. There’s less of a need to hallucinate when there’s access to up-to-date information in real time. The Responses API allows developers to implement web search with only a few lines of code.

For example, if you wanted your LLM to look up notable new book releases, you could simply insert the following JavaScript into your code:

const response = await openai.responses.create({
model: "gpt-4o",
tools: [ { type: "web_search_preview" } ],
input: "What are some notable books that came out this week?",
});
console.log(response.output_text);

The web search tool offered by the Responses API works similarly to any other response by an LLM. It uses a simple benchmark to analyze the accuracy of a response. The model returns only responses with an accuracy score of 88% or greater.

The returned responses also include sources, allowing the user to learn more about how the LLM determined its response.

Responses API also lets you query files on your local system or network. It offers support for many different file types, which can then be filtered by metadata, ranked, or sorted.

Here’s an example of a Local File Search for searching a corpus of texts:

// First, create a vector store with uploaded files
const corpusTexts = await openai.vectorStores.create({
name: "Corpus Texts",
file_ids: [file1.id, file2.id, file3.id], // Assume these files are already uploaded
});

// Now query it using the Responses API
const response = await openai.responses.create({
    model: "gpt-4o-mini",
    tools: [{
        type: "file_search",
        vector_store_ids: [productDocs.id], // Correctly referencing the ID
    }],

    input: "What is the meaning of life?",
});

console.log(response.output_text);

The File Search function is one of the most useful features for a custom LLM. It lets users interact with whatever documents they search, making highly advanced virtual assistants and chatbots possible.

Computer Use

The Responses API isn’t restricted to returning text queries. It can also replicate basic computer functions, like mouse movements and keyboard actions, similar to automations in Python. The LLM just makes all of the movements instead of recording users’ actions.

Computer use is another built-in tool that works with the Responses API. Here’s an example of how to implement Computer Use actions in code:

const response = await openai.responses.create({
    model: "computer-use-preview",
    tools: [{
        type: "computer_use_preview",
        display_width: 1024,
        display_height: 768,
        environment: "browser",
    }],
    truncation: "auto",
    input: "Can you help me pick out the right guitar?",
});

console.log(response.output);

The Computer Use function further bridges the gap between AI, retrieval-augmented generation, and robotic process automation, the umbrella term for automated tasks like web scraping or data entry.

Agents SDK

OpenAI’s Agents SDK allows users to easily integrate agentic AI tools into their code, making it simple to switch between agents, monitor transactions, and install guardrails. Here’s an example of how to use the agents library once you’ve installed Agents SDK:

from agents import Agent, Runner, WebSearchTool, function_tool, guardrail

@function_tool
def submit_refund_request(item_id: str, reason: str):
    # Your refund logic goes here
    return "success"

support_agent = Agent(
    name="Customer Service Representative",
    instructions="You are a Customer Service Representative. Your job is to be helpful and understanding [...]",
    tools=[submit_refund_request],
)

shopping_agent = Agent(
    name="Personal Shopper",
    instructions="You are a shopping assistant that's able to search the web [...]",
    tools=[WebSearchTool()],
)

switchboard = Agent(
    name="",
    instructions="Route the user to the correct agent.",
    handoffs=[shopping_agent, support_agent],
)

output = Runner.run_sync(
    starting_agent=Switchboard,
    input="Which guitar should I get?",
)

This Python script imports the Agent library and then uses it to create three agents. It sets the default agent as Switchboard. It then asks the agent for advice about buying a guitar.

Getting Started With Agents SDK

We’ll conclude by showing you how to get started with the Agents SDK so you can try out agentic AI in your own code and projects.

Start off by cloning the OpenAI GitHub Repository:

git clone https://github.com/openai/openai-agents-python

Once that’s finished installing, create a virtual environment and then activate it.

python -m venv env
source env/bin/activate

Next, install the Agents SDK.

pip install openai-agents

Once the Agents SDK is installed, you can run your trial code. Just make sure to set your API key environment variable. (You can get one here if you don’t have an OpenAI API key already.

Installing Guardrails and Agentic Handoff With Agents SDK

Running automated agents without any supervision or limits is a mistake. Guardrails help ensure your AI agents operate safely and securely while remaining autonomous. They can also help determine a user’s intent, making them a convenient way to determine which agent will best serve a user’s needs.

OpenAI’s guardrails run parallel to your agents, allowing each interaction with the AI to be monitored and validated. For example, you might use guardrails to route generalized traffic towards free or inexpensive services rather than more costly tools.

To implement guardrails in your OpenAI application, start off by installing the necessary libraries.

pip install pydantic-ai openai-agents nest-asyncio google-colab

Now you can import nest_asyncio, which allows you to run nested versions of asyncio, as well as use the google-colab library, which we’ll use to import user data.

from agents import Agent, InputGuardrail, GuardrailFunctionOutput, Runner

from pydantic import BaseModel

from google.colab import userdata

import asyncio

import os

os.environ["OPENAI_API_KEY"] = 
userdata.get("OPENAI_API_KEY")

Make sure to set your OPENAI_API_KEY environment variable, either by using the SET command in the terminal or by saving a .env file in your development directory. If you decide to use the .env method, you’ll need to install the python-dotenv library, as well. Then you can invoke the API key using the load_dotenv() function after you import the dotenv library.

Once that’s finished, you can configure the guardrails, similar to how you set up the agents. You start by defining agents for both guardrails and handoffs.

cs_research_agent = Agent(

    name="CS Researcher",

    handoff_description="Specialist agent for Computer Science Research",

    instructions="You provide help with CS research. Explain your reasoning at each step and include examples.",

    )

    bio_research_agent = Agent(

        nam="Biology Researcher", 

        handoff_description="Specialist agent for Biological Research", 

        instructions="You assist with biological research. Explain important events and context clearly."
   )

Now you can set up the guardrail triggers.

async def research_guardrail(ctx, agent, input_data): 

    result = await Runner.run(guardrail_agent, input_data, context=ctx.context)

    final_output = result.final_output_as(ResearchOutput)

    return GuardrailFunctionOutput(

        output_info=final_output,

        tripwire_triggered=not final_output.is_research,

    )

Next, you’ll set up the agent handoff.

triage_agent = Agent( 

    name="Triage Agent",

    instructions="You determine which agent to use based on the user's research question", 

    handsoff=[cs_research_agent, bio_research_agent],

    input_guardrails=[

        InputGuardrail(guardrail_function=research_guardrail),

        ],

    )

As we near the end, we’re going to set up explicit tracing.

from agents.tracing.setup import GLOBAL_TRACE_PROVIDER 

from agents import set_tracting_export_api_key

set_tracing_export_api_key(OPENAI_API_KEY)

GLOBAL_TRACE_PROVIDER._multi_processor.force_flush()

Finally, we’re ready to run the agent.

async def main():

    result = await Runner.run(triage_agent, "What is Agentic AI?"

    print(result.final_output)

if __name__== "__main__":

    asynchio.run(main())

Orchestrating Smarter Agents With OpenAI

As you can see, agentic AI is an efficient way to implement all manner of logic in your code, which can sometimes be tricky to realize. This segmentation can be used in many valuable ways, particularly regarding AI and LLMs.

Using OpenAI tools to build agentic AI allows you to easily customize the model for different applications, allowing you to use one LLM for many different applications. Setting up agents allows you to easily train agentic AI on specific data, which can be endlessly useful for setting up everything from chatbots to customer service agents to well-trained research assistants.

Even better, guardrails allow users to easily switch between different Agents for different applications without developers having to write a ton of code to realize its logic. Add in native support for searching the web and local files, and OpenAI’s tools for agentic AI promise to correct many of AI’s initial shortcomings.