Rails World 2026: Agents on Rails: Building AI Pipelines in plain Ruby (Daniela Velasquez) Video: https://www.youtube.com/watch?v=OX5coKfra4c Talk page: https://www.rubyevents.org/talks/agents-on-rails-building-ai-pipelines-in-plain-ruby Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- Hello, everybody. This is Agents on Rails, and I want to talk about how you can build AI pipelines using plain Rails. So we have Ruby on Rails, but how can we put Agents on Rails? And before we dive into the how, let's think a little bit about the why. We know that a lot of applications have tons and tons of data, and within that data, there is very interesting things that you can find. But nobody really has the time to sit and look at all this data and make sense of it with what is in the data and what is around us. And we have come up with, like, cool ideas to try to figure this out, like dashboards or maybe chatbots where you can just connect all that data to your applications and see what happens. But what if we take a step forward and we don't wait for users to have to come to us, and instead we go to them, because sometimes you don't know what you don't know, and we don't want them to have to think as much, and sometimes there might be ideas that are hard to track. So we're going to pretend for the exercise of this presentation that we have an application where we're tracking and an athlete's workouts, and we're going to have an athlete's workouts, and their sleep locks, and their sleep locks, and their headbeats, and we're going to try to coach them and help them before they even have to come to us. So it all is going to start with a job. So we're going to have a researcher job, which is a simple job, and what are we doing research on? An athlete. Next, we're going to have a prompt, which is the heartbeat of the LLM. In our case, we're going to say you're a running coach, study these athlete's workouts, sleep and heart rate, then propose concrete changes to the training for next week for next week, back every change for the data you actually look at. You can make this as long or as short as you want to, and obviously tailor it to your application. Then next, you're going to need an LLM provider. There are many out there. There's Google, there's Entropic, there's GPT. I'm not trying to promote anything here, but choose whatever you want and whatever you can afford, because they are expensive. And to that provider, you're just going to pass your prompt. Then you're going to give it access to some tools. So in one of these, we're going to have a workout history, sleep log, a weather forecast, and a web search. These tools are not mandatory for the agent to use, so they can come up with ideas on where they might suit you better, they might suit them better to be used. And as you can see, we're not just looking at data within our application, but also giving it access to things outside the application itself, so you can look at the world around this athlete and make connections with that data. Next, we have a model, which is basically the brain you're trying to rent. And some brains to rent. And some brains are bigger and more expensive than others. And in this case, you really don't need a big, fast brain. You just need a slow one, but a big one, because you do want it to be really thinking about it, because it's doing all the research. And then you're going to come up with the reasoning, and this is how hard you want it to think before it starts coming up with answers, and the higher the reasoning, the more confidence you're going to have. So, you're going to have a job, and you're going to have a job, and you're going to have a job, and you're going to have a job, and it's doing all these calls, looking through the data, calling tools, and preparing to give you an answer. But this job is going to take a very long time. It can be between 10, 15, 30 minutes, depending on how many tools the prompt is. So what happens when there is a deploy? As soon as you make a deploy, which I assume most of you do continuous deployments, your agent is going to go back to zero because it lives within a job. And it will go back to zero. And because the calls to the LLM providers are stateless, they won't remember what was happening. So not only did you waste a lot of time, you also wasted a lot of money, and your bosses are probably not really happy. So how can we fix this? Let's go back to the researcher job, and we're going to have still our athlete, but we're also going to pass a conversation, which is going to help us track all the messages we've exchanged with the LLM providers. We're going to have a prompt as we did earlier, and we're going to have this. If the conversation has no messages, we're going to have this. If the conversation has no messages, we're going to store the first message as the prompt with the role of the system, another message as if you are an user and you're telling, hey, do research for this athlete. Next, you're going to go into a loop, and you're going to do exactly as we did before. We're going to call an LLM provider with the tools, but notice that we're not passing just the prompt. Now we're passing all the messages from that conversation. So that if your job needs to restart, it can remember all the messages that had already been passed back and forth. It also allows you to store multiple messages the LLM provider has given you throughout the thinking process. And obviously every time you get a response, you're going to save it into your conversation messages, and you need a way to know when you're done so you can come out of the loop and finish your job. So now that our agent is actually working, let's play pretend what the agent might have come up with. So now that our agent might have come up with. Tuesday rounds, we're going to start an hour later. Tuesday rounds, you're going to sleep, start them an hour later. Add a two-for-five-minute bike ride this week to build an aerobic base. Your long run starts to fast and fade, ease the first kilometers. And heavy rain forecast Saturday moves your long run to Sunday morning. This all seems pretty good to me. As you can see, it's doing some ideas just looking at the data and some other ideas are coming maybe from the web search or the forecast tool that it was using. So that's how you can. So that's how you put agents on Rails. And that was kind of easy, right? You let an LLM come up with ideas, and then you show them to your users. But as many of you probably know, it's hard to trust them. They sometimes come up with very random ways of doing, very creative ways of not the right thing that you ask them to do. And honestly, half the time, I feel like they're just trolling you for what you're asking them to do. When we first started doing this at Shopify, I remember going through the data of the data of the suggestions the researcher was finding, and I found this very interesting one that first didn't look that interesting, which was bundle your Waba shampoo and Waba conditioner because they're usually bought together. That sounds fine. Then I continued reading, and I found the same suggestion again. And I thought, huh, maybe I'm missing on some sort of trend with this Waba conditioner and shampoo. Who want Waba on their hair? I don't know, but to each their own. And then I continued reading, and then I found it again. And so I got curious, and I went to look what this merchant actually sold, and they were selling brass. And then I continued looking, and this was a problem throughout the entire researcher doing it for multiple shops. And then I was trying to figure out how did we come up with a Waba problem? Why did he continuously was asking people to bundle their non-existent Waba shampoo and Waba conditioner? Then I realized that it was on the prompt. It was an example. And it was going to make it as an example and give it as an example and give it as an example and give it as a suggestion. That's one way where they just, you know, just do their own thing. And so, inevitably, they fall off the rails. And not in the rails way, but more in the making sense kind of way. So how do we fix it? How can we trust our agents? Well, we're going to throw more LLMs at the problem. But these LLMs, they are going to act as judges. And they're going to judge the output of the researcher. And they're going to judge the output of the researcher. And they're going to judge the output of the researcher. And these new LLMs have a much simpler job. They're basically just a classifier, and they're only going to care about answering one question that matters to your application. Let's pretend that this guy is going to catch made up numbers. This is going to make sure we're being kind. And this is going to do a reality check, just making sure that what they're telling them to do, they can actually do it. So you must be kind. So you must be thinking, okay, we had one LLM that we obviously cannot trust. And now you want to add three more. How does that make any sense? Also, before I continue, just so you know, you don't have to have three judges. You can have more, but you can also have less. But this is how we're going to trust them. We're going to do the exercise just with the kind judge. And you're going to come up with 200 examples of being kind and 200 examples of not being kind. So, for example. Your uphill splitter challenging. Swap one easy run for a chill session. That's kind. Seven runs in seven days. Stop before you break yourself. Maybe not super kind. Use different shoes. You've run with the same shoes 900 kilometres. No wonder your knees hurt. That's not super kind. Also, I should have used miles. So, if you don't know the conversion, just assume it's many, many miles. Fueling strategy. Your pace drops after 60 minutes. Grab a snack at the 45-minute mark. That's fine. That's fine. You cram. You cram baby around over 15 kilometers. That's fine. You cram baby around over 15 kilometers. Didn't water like an adult. Maybe not super kind. Which reminds me. Okay. So, you're going to come up with 200 examples of this. And because you know which ones are kind and which ones are not, now you're going to come up with a prompt for your MLM. In this case. In this case, we're going to say, hey. In this case, we're going to say, hey. In this case, we're going to say, hey, you review training suggestions before an athlete sees them. Answer one question. Would a support coach say this? Blame, sarcasm, address, fail, honest data delivered kindly passes, reply with pass or fail, and one sentence why. Now that you have your prompt, you're going to run that prompt through each example on your data set so you can measure how accurate it is. What's interesting about this is you can try different prompts. You can also try different models and figure different models and figure different models and figure out which one fits you better. Also, remember that these are smaller models and you want them to be faster, so you don't need to choose the most expensive model out there and you don't need really high reasoning for this. So, once your researcher is done giving you suggestions, you're going to move to the second step of your pipeline, which is running all these judges for each of the suggestions. We're going to do the exercise again, just with the kind judge. What are we judging? We're going to do the suggestion. We're going to pass a prompt, same as we saw earlier. And then you're just going to have, again, just an LLM provider. You're going to pass your two messages. Here's the prompt. This is what you need to judge. And you're going to just have a model and the reasoning. In this case, you really don't need high reasoning. Remember, you want them to be cheap and fast. And once that's done, you just save that information. So, remember what the researcher produced earlier. Once this is done, once this is done, we're going to run all our judges. And maybe the kind judge will say, hey, telling them that it's their worst leave doesn't sound super kind. The next one, the fat checking might tell us, hey, they don't own a bike, so they cannot do this. And you continue doing that through each of the actions. And then you realize that there are two that you cannot really trust to show. One, because it's maybe not being kind, and the other one, because they simply cannot do it if they don't own a bike. So that you cannot do it. So that's how you cannot do it. So that's how you put agents on rails. So let's do a recap. We had an athlete with a bunch of information and metrics, and we're going to give that to the researcher agent. And the researcher is going to come up with personalized coaching ideas based on that data. In our case, it came up with four. And then after you have all that, your second batch of jobs is going to make sure that you evaluate each and one of these with the judges that fit your application. And you do that for all of them. And you do that for all of them. And each job is going to give you a thumbs up or thumbs down based on the metric they care about. And then in the end, you're only going to show these two to your athlete. It's ultimately what they see. However, users are going to have thoughts and feelings, especially after you tell them that this was AI generated. So what if the athlete says something like, I work on Sundays from 6:00 a.m. to 3:00 p.m. Well, that's something. Well, that's something that no matter how many tools you give it, you might have not know or didn't have in your data. And that's something that your researcher wants to know so that next time the pipeline is running, it can remember that this happened. So how are we going to do this? Well, we're just going to have a simple class. There's an application record to store that feedback. Belongs to an athlete. Belongs to a suggestion. And you remember the researcher job. We just put it in the prompt and we say, what the athlete is what the athlete has told you before so that the researcher can learn from it. But the problem is, if you put raw feedback of what your users are telling you, you might run into some issues because LMs really thrive on patterns. And in our initial example, she was being nice. But what if she's not nice? You know, some of us do some name calling sometimes for agents because it's frustrating. We have a kind judge, we have a kind judge. We have a kind judge, but you don't really want to spend tokens having a researcher just to produce really angry suggestions to your users. And you know, it might pick up on the VAT vibes. You don't want to do that. So any ideas on how we might solve this? Another agent. So you're going to pass that raw user feedback and give it to an agent that is just going to take all that noise and you just want the fact. So no matter how your user tells you their frustrations, you don't want the fact that you don't have to do that, you don't have to do that, you don't have to do that, you don't have to do that. And you just want to remember that they work from six to three on Sundays. And now if we go back to our feedback class, what we're going to do is we're going to use a callback. I know callbacks I don't really love by a lot of people, but I love callbacks. So you use them and you send out a job, and that job is going to be very similar to the ones we've seen. You take your feedback, you pass a prompt, and after you rejected a training suggestion, in one sentence, you say what the coach should remember about this athlete, so it doesn't happen again. And then you do as you were doing earlier, you just pass to the LLM provider that message or the suggestion was, also it needs to be a simple model, so small brain, low reasoning, and you're going to save that as the process text of your feedback. And then if we go back to the researcher job, that's what you really want to show there. And this is where it also gets interesting, because you have to think about like, what about when they switch jobs, what if they don't work on Sundays anymore? And this is where you get into like the dreaming kind of phase for agents, where you need to figure out that those facts that you've extracted make sense over the long run. And this is just a challenge that we're all going to have to face as we continue to capture information through LLMs. So now let's do an actual recap. We had a researcher that produced some Then we get our judges to make sure that they pass things that our application cares about, and we show that to our athlete. Once the athlete sees them, they might have some feedback to give us, and we're going to put a feedback loop in there where we process that and give it back to the agent so they can continuously learn throughout time. And that becomes the challenge of how do we keep that updated over time. But for now, for the pipeline that we're working on, that's it. Thank you. And just a full disclaimer, I did all these images with AI, and there were some fun behind the scenes that happened there, like this one, which was pretty scary and weird. This one where I told it agents, and I think it assumed like cops, people. And then this one where it just refused to remove the big guy from the background. Also, if you did enjoy the little agents, I made a bunch of stickers, so please go find me and I'll share them with you. Thank you.