r/rails 7d ago

How to cleanly stream LLM tokens in Rails 7/8 without complex JS frameworks

Most tutorials suggest setting up a separate Node microservice or heavy SPA frontend just to handle streaming AI responses.

You don't need that. You can stream token-by-token directly inside a pure Rails stack using ActionCable + Turbo Streams:

Wrap your API call (ruby-openai or anthropic) inside a dedicated Service Object.

Pass a chunk-handling block that broadcasts incoming tokens via Turbo Streams.

Target the DOM element directly—zero custom JavaScript state needed.

Keeps your codebase maintainable, fast, and native.

Upvotes

3 comments sorted by

u/mnordin 6d ago

Yes, I do this too 🤝

It’s in the rubyllm docs: https://rubyllm.com/streaming/#rails-with-turbo-streams

u/irosh24a 6d ago

Indeed 🤝