Connect to AG-UI
Connect AG-UI with existing protocols or custom solutions
In this tutorial, you’ll learn how to create an agent that connects to OpenAI’s API and demonstrates how the Agent User Interaction Protocol (AG-UI) can integrate seamlessly with any modern AI service. Let’s get started.
Prerequisites
Make sure to have the following installed:
Setting up OpenAI access
Before we start coding, make sure you have an OpenAI API key. You can get one by signing up at OpenAI’s platform.
Once you have your API key, set it as an environment variable:
Bridge OpenAI with AG-UI
Now let’s demonstrate how AG-UI can connect to a modern AI service by implementing a bridge to OpenAI’s API.
Project Setup
Let’s create a new project for our AG-UI bridge:
AG-UI Recap
The Agent User Interaction Protocol is a framework that lets you connect agents
to clients in a structured, event-driven way. Agents implement the
AbstractAgent
class from @ag-ui/client
and emit events for each piece of
content, progress, etc.
OpenAIAgent
We’ll create a special agent class, OpenAIAgent
, which:
- Connects to OpenAI’s API using their official Node.js SDK.
- Sends the user’s messages to OpenAI.
- Streams the response back, emitting AG-UI events for each chunk of text.
Create a file called src/openai-agent.ts
:
Explanation
-
API Connection: We create an OpenAI client using the official SDK and connect to OpenAI’s API using your API key.
-
Message Formatting: We convert AG-UI messages to the format expected by OpenAI.
-
Emitting Events:
RUN_STARTED
means the agent started processing the request.TEXT_MESSAGE_START
/TEXT_MESSAGE_CONTENT
/TEXT_MESSAGE_END
together send the agent’s textual response to the AG-UI pipeline.RUN_FINISHED
means this run is over.
-
Stream Processing:
- We create a streaming completion request to OpenAI.
- For each chunk of text received, we emit a
TEXT_MESSAGE_CONTENT
event. - After all chunks are processed, we finalize the message and run.
Putting It All Together
Create a main entry point file src/index.ts
that sets up our AG-UI agent:
Run the application:
Bridging AG-UI to Any Protocol
This example demonstrates how to bridge AG-UI with an external AI service. The key components are:
- AG-UI Agent Implementation: Create a class that extends
AbstractAgent
and implements therun
method. - API Integration: Connect to an external API (in this case, OpenAI).
- Message Formatting: Convert between AG-UI’s message format and the API’s expected format.
- Event Streaming: Stream the API responses back as AG-UI events.
You can apply this pattern to connect AG-UI to virtually any AI service or protocol:
- HTTP/REST APIs
- WebSockets
- MQTT for IoT devices
- any other protocol
By following this pattern of creating protocol adapters around your AG-UI agents, you can make your AI agents available through any AI service while maintaining a consistent agent implementation.
Conclusion
Congratulations! You’ve created an agent that connects to OpenAI’s API and bridges it to the Agent User Interaction Protocol. This tutorial demonstrates how easy it is to adopt AG-UI with any modern AI service—providing a consistent, streaming interface for your applications regardless of the backend provider.
Connect Your Agent to a Frontend
Now that you’ve built your AG-UI compatible agent, it’s ready to be connected to any frontend that supports the AG-UI protocol. One such frontend is CopilotKit, which provides a rich set of UI components designed to work seamlessly with AG-UI agents.
To connect your agent to CopilotKit:
- Follow the CopilotKit documentation to set up your frontend
- Configure CopilotKit to connect to your agent using the AG-UI protocol
- Enjoy a fully functioning chat UI with streaming responses!
With this setup, you can focus on building powerful agent capabilities while leveraging existing UI components that understand the AG-UI protocol, significantly reducing development time and effort.