Integrating Dify AI into a Rails Backend for Personalized Ordering

2026-05-23

A practical walkthrough of wiring a Dify RAG workflow into a Ruby on Rails backend — covering API design, prompt context injection, and streaming responses to the frontend.

For Aiorder's personalized ordering assistant, we needed an AI layer that understood each user's order history and the current menu in real time. Dify's workflow engine was the right tool — but integrating it cleanly into Rails took some deliberate design.

Dify as the AI Layer

Dify provides a visual RAG workflow builder where you can define knowledge bases for menu items and user preferences, a retrieval strategy covering semantic search, keyword, and hybrid modes, and LLM prompt templates with dynamic variable injection. Our Rails backend's job was to be the orchestration layer between the user, our database, and Dify.

Context Injection Pattern

Each API call to Dify included a context payload built from the user's last 10 orders, the current table's active menu, and the time of day along with any active promotions. Keeping this payload lean was important — Dify's token window fills fast with large history arrays.

Streaming Responses via SSE

To give users a ChatGPT-like experience, we streamed Dify's response chunks back to the React frontend using Server-Sent Events, keeping the connection open until the full response was delivered.

Lessons Learned

Keep context payloads small. Cache menu data aggressively since it changes infrequently but is queried on every request. Monitor Dify workflow latency independently from Rails response time to isolate bottlenecks.