Build with AG-UI
Implement AG-UI events directly in your agent framework
In this tutorial, you’ll learn how to build an HTTP endpoint that is compatible with the AG-UI protocol.
Prerequisites
Make sure to have Python and Poetry installed.
Setup a New Project with Poetry
First, let’s create a new project and set up Poetry for dependency management:
Install Dependencies
Now, let’s install the necessary packages:
Create a Basic Endpoint with FastAPI
Create a new file called my_endpoint/main.py
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AG-UI Input
Next let’s update our endpoint to properly parse the incoming AG-UI request
using the RunAgentInput
Pydantic model:
FastAPI automatically validates the incoming request against the RunAgentInput schema. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AG-UI supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Awesome! We are already sending RunStartedEvent
and RunFinishedEvent
events,
which gives us a basic AG-UI compliant endpoint. Now let’s make it do something
useful.
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AG-UI events:
You’ll need to set your OpenAI API key as an environment variable and then restart the server:
This implementation creates a fully functional AG-UI endpoint that processes messages and streams back the responses in real-time.
Prerequisites
Make sure to have Python and Poetry installed.
Setup a New Project with Poetry
First, let’s create a new project and set up Poetry for dependency management:
Install Dependencies
Now, let’s install the necessary packages:
Create a Basic Endpoint with FastAPI
Create a new file called my_endpoint/main.py
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AG-UI Input
Next let’s update our endpoint to properly parse the incoming AG-UI request
using the RunAgentInput
Pydantic model:
FastAPI automatically validates the incoming request against the RunAgentInput schema. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AG-UI supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Awesome! We are already sending RunStartedEvent
and RunFinishedEvent
events,
which gives us a basic AG-UI compliant endpoint. Now let’s make it do something
useful.
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AG-UI events:
You’ll need to set your OpenAI API key as an environment variable and then restart the server:
This implementation creates a fully functional AG-UI endpoint that processes messages and streams back the responses in real-time.
Prerequisites
Make sure to have Node.js (v16 or later) and npm or yarn installed.
Setup a New Project
First, let’s create a new project and set up npm with TypeScript:
Install Dependencies
Install the necessary packages:
Create a Basic Endpoint with Express
Create a new file called src/server.ts
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AG-UI Input
Next let’s update our endpoint to properly parse the incoming AG-UI request
using the RunAgentInput
schema:
Express with zod validation ensures the incoming request conforms to the AG-UI protocol format. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AG-UI supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AG-UI events:
You’ll need to set your OpenAI API key as an environment variable and then start the server:
This implementation creates a fully functional AG-UI endpoint that processes messages and streams back the responses in real-time.
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.