Skip to content
RailsDev.io

Bring AI Agents Into Your Rails UI with the AG-UI Protocol

Learn how the AG-UI protocol lets Rails apps treat AI agents as interactive frontend users, with patterns for chat, forms, approvals, and real-time UI feedback.

Diagram: Rails UI → AG-UI Hub → AI Agent → Backend API
On this page
  1. Why Your Rails UI Still Treats AI Like a Backend Tool
  2. What the AG-UI Protocol Actually Is
  3. The Mindset Shift: Agents as First-Class Frontend Users
  4. Implementing AG-UI in Rails: A Practical Walkthrough
  5. Real-World Scenarios Where This Shines
  6. Performance and Operational Considerations
  7. Verification Note
  8. Frequently asked questions
  9. Conclusion
  10. The Bottom Line

The AG-UI protocol Rails integration pattern treats an AI agent as an interactive participant in your frontend rather than a backend text generator. By streaming typed events (RUN_STARTED, TOOL_CALL_START, STATE_DELTA) over SSE or ActionCable per the AG-UI specification, a Rails app lets an agent update forms, trigger Turbo Streams, and ask for user approval without bespoke endpoints for every widget.

Why Your Rails UI Still Treats AI Like a Backend Tool

I’ve been building Rails applications for over a decade, and I’ve watched the wave of AI integrations roll in with a familiar pattern: we bolt a chat widget onto the page, wire it to some LLM endpoint, and call it a day. The frontend team writes a React component that streams tokens; the backend team writes a thin controller that proxies to an AI service. Six months later, the product team asks, “Can the AI help the user fill out this form?” and we end up with a dozen bespoke endpoints that each handle state differently, validate differently, and fail differently. The AI never really sees the UI—it just spits text into a box.

The problem runs deeper than convenience. Every AI feature we bolt on is a second-class citizen: it can’t click a button, can’t read the current scroll position, can’t trigger a Hotwire Turbo Stream action without us writing special-case code. We treat the AI as a backend process that happens to talk to the frontend, instead of treating it as a peer that lives in the UI layer. That’s why I’ve become a fan of the AG-UI protocol—it’s the first specification I’ve seen that flips the script and says, “The agent is a frontend user, too.”

What the AG-UI Protocol Actually Is

AG-UI (Agent-User Interaction) is an open, event-driven protocol that standardizes how AI agents communicate with user-facing applications. It’s not a framework; it’s a contract. The agent emits a stream of typed events—RUN_STARTED, TEXT_MESSAGE_CONTENT, TOOL_CALL_START, STATE_SNAPSHOT, and so on—and the frontend consumes those events to update the UI in real time. Crucially, the protocol is bidirectional: the frontend can send input to the agent, and the agent can respond with state changes that the frontend applies.

This is a radical departure from the typical “chat widget” pattern. In AG-UI, the agent doesn’t just return text; it returns a structured stream of actions. It can say, “I’m starting a run,” then “Here’s a tool call to fetch user data,” then “Here’s the state snapshot of the form I just updated,” then “Here’s the final answer.” The frontend just needs to know how to interpret those events, and the agent needs to know how to emit them. Everything else—routing, authentication, persistence—stays in your Rails domain.

The Mindset Shift: Agents as First-Class Frontend Users

When I tell Rails teams that AG-UI lets you treat AI agents as first-class frontend users, I see the skepticism roll in. “But agents don’t have browsers,” they say. “They don’t have sessions.” And they’re right—agents don’t need browsers. They need a protocol that lets them participate in the UI without simulating a human. That’s exactly what AG-UI provides.

Think about it: a human user interacts with your Rails UI through clicks, keystrokes, and page navigations. An AI agent interacts through events. If you design your UI to be event-driven—which Rails apps already are, thanks to Hotwire Turbo Streams and Stimulus—then an agent can become just another source of events. It can trigger a Turbo Stream that updates a partial, it can push a Stimulus action to a component, it can even mutate the same Redux-style state that human-driven interactions use. The agent becomes a peer in the component tree, not a distant service.

This has three immediate benefits:

  1. Consistency – The same UI updates that happen when a human clicks a button happen when an AI agent decides to make a change. No special-case code.
  2. Reduced glue – Instead of writing a custom endpoint for every AI feature, you write one AG-UI adapter and let the agent speak the same language as your frontend.
  3. Collaborative interfaces – You can build experiences where the AI and the human are co-editing the same state, with the protocol keeping everything in sync.

Implementing AG-UI in Rails: A Practical Walkthrough

The good news is that you don’t have to build the protocol from scratch. The ruby_llm-ag_ui gem provides a Rails integration that handles the heavy lifting. Here’s what the setup looks like.

First, add the gem to your Gemfile:

ruby
gem 'ruby_llm-ag_ui'

Then run the generator:

bash
bundle install
bin/rails generate ruby_llm:ag_ui:install
bin/rails db:migrate

This creates a migration for a durable run store (ActiveRecord) and an initializer that wires everything up. The store is critical—it allows runs to survive deploys and lets multiple clients reconnect to the same conversation.

Next, you mount the AG-UI server in your routes. The gem provides a mountable Rack application:

ruby
# config/routes.rb
mount RubyLLM::AgUI::Rails::Server.new do |run|
  MyAgent.events_for(run)
end, at: '/ag_ui'

The block you provide receives a Run object and must return an enumerable of agent events. In a real app, this is where you’d instantiate your LLM, attach any context (like the current user’s records), and stream the response.

The controller concern is even simpler. You include the RubyLLM::AgUI::Rails::Streaming module in your controller, and it gives you a stream_ag_ui_run method that handles the SSE plumbing:

ruby
# app/controllers/agent_runs_controller.rb
class AgentRunsController < ApplicationController
  include RubyLLM::AgUI::Rails::Streaming

  def create
    stream_ag_ui_run { |input| chat_for(input) }
  end

  private

  def ag_ui_scope
    "tenant:#{current_tenant.id}:actor:#{current_user.id}"
  end

  def ag_ui_state
    { 'orders' => current_user.visible_orders.map(&:as_json) }
  end
end

The ag_ui_scope method is mandatory—it scopes every run to the current user, preventing cross-tenant access. The ag_ui_state method is optional; it returns the state that gets published when a client reconnects to a completed run. This is what allows the UI to show the latest state of a form or a dashboard after the agent has finished its work.

Real-World Scenarios Where This Shines

I’ve started using AG-UI in three types of applications, and the results have been consistent:

Interactive dashboards. Imagine a sales dashboard where the AI agent can filter data, change chart types, and even generate summaries—all without the human touching a dropdown. The agent emits STATE_DELTA events that update the chart component, and the human sees the changes happen in real time.

Form assistance. Instead of a chatbot that says “Try entering your email,” the agent can actually fill in the email field, trigger validation, and even correct errors. It does this by emitting events that the form component interprets as user input, making the agent feel like a ghost in the machine.

Collaborative editing. Two humans and an AI editing a document together. The AI suggests changes, the humans accept or reject them, and the protocol keeps everyone’s view in sync. This is where AG-UI really shines—it’s built for bidirectional, stateful interaction.

Performance and Operational Considerations

Now, let’s talk about the trade-offs. AG-UI is not a silver bullet, and I’ve seen it misused.

Threading. The default transport is ActionController::Live, which holds a thread for the life of the run. If you have long-running LLM calls, you’ll quickly hit thread limits. In production, you’ll want to offload the work to a background job and have the controller read from a broker (like Redis or Kafka). The gem gives you the pieces to do this, but you have to wire it up yourself.

Proxies. Any intermediary that buffers SSE streams will turn your real-time events into a single response at the end. If you’re behind nginx, make sure X-Accel-Buffering: no is set. Test with curl -N to see if your infrastructure is actually streaming.

When not to use it. If your AI feature is just a chatbot that returns text, AG-UI is overkill. The protocol adds complexity that you don’t need. Save it for when the agent needs to interact with the UI beyond just displaying text.

Verification Note

The specific implementation details of the ruby_llm-ag_ui gem (such as the exact migration schema, the behavior of the ag_ui_state method, and the performance characteristics of ActionController::Live under high concurrency) are based on my hands-on experience with the gem in development and staging environments, but I have not run production load tests against it. The AG-UI protocol specification (event types, state management rules) is publicly documented at docs.ag-ui.com, but the protocol is still evolving, and some event types may change. Always check the official documentation for the latest version before committing to a production rollout.

Frequently asked questions

What is the AG-UI protocol?

AG-UI (Agent-User Interaction) is an open, event-driven protocol documented at docs.ag-ui.com that standardizes how AI agents stream messages, tool calls, and state updates to user-facing apps and receive user actions back.

Do I need React to use AG-UI with Rails?

No. Because AG-UI is a transport-level event contract, Stimulus controllers or Hotwire Turbo Streams can consume the same SSE or WebSocket events as a React frontend.

When should a Rails team skip AG-UI?

Skip it for plain question-and-answer chat or background tasks. A standard streaming controller or background job is simpler when the agent never mutates UI state or requests in-form approvals.

How does AG-UI handle multi-user security in Rails?

Runs should be scoped per authenticated user (ag_ui_scope) before streaming or replaying STATE_SNAPSHOT payloads, using the same Devise or session authorization your controllers already enforce.

Conclusion

AG-UI turns the Rails UI into a shared surface that both humans and agents drive through one typed event contract instead of ad-hoc polling endpoints.

If you’re planning an interactive agent interface alongside your Rails stack, see my AI software development services or book a discovery call to scope the architecture.

The Bottom Line

AG-UI is not just a technical protocol; it’s a philosophical shift. It forces you to think of your Rails UI as a platform that multiple actors—humans and AI agents—can interact with through a shared language. That’s a much more sustainable approach than building a new integration pattern for every AI feature.

If you’re already deep into Rails AI integration, I’d suggest reading up on the broader ecosystem in Ruby on Rails AI Integration: A Practical Guide for Rails 8 with RubyLLM. And if you’re thinking about exposing your app to external agents, the Rails MCP Server post is a good companion piece—it covers a different protocol, but the goal is the same: making your Rails app a first-class participant in the AI world.

Start small. Pick one form, one dashboard, one collaborative feature. Wire it up with AG-UI and see how it feels. I think you’ll be surprised at how much cleaner the code becomes when your AI agents are treated as frontend citizens, not backend parasites.

Keep reading