Route to Success: Scalable Routing Agents With Databricks and DSPy
Summary
- Routing agent pattern: A routing agent intelligently directs questions to the best AI assistant for each task, enabling organizations to manage multiple AI systems through a single interface.
- Optimization with DSPy: This video shows how to use DSPy optimizers to maximize routing accuracy while reducing latency through smaller model optimization on the Databricks Data and AI platform.
- Enterprise scalability: This video covers designing for growth, stateful interactions, authorized access, and real-world performance monitoring for supporting tens or hundreds of AI assistants at scale.
Route to Success: Scalable Routing Agents With Databricks and DSPy

Talk by Luis Moros, Staff Data Scientist - ML Practice, Databricks
As companies increasingly adopt Generative AI, they're faced with a new challenge: managing multiple AI assistants. What if you could have a single, intuitive interface that automatically directs questions to the best assistant for the task? Join us to discover how to implement a flexible Routing Agent that streamlines working with multiple AI Assistants. We'll show you how to leverage Databricks and DSPy 3.0 to simplify adding this powerful pattern to your system. We'll dive into the essential aspects including: Using DSPy optimizers to maximize correct route selections Optimizing smaller models to reduce latency Creating stateful interactions Designing for growth and adaptability to support tens or hundreds of AI Assistants Ensuring authorized access to AI Assistants Tracking performance in production environments We'll share real-world examples that you can apply today. You'll leave with the knowledge to make your AI system run smoothly and efficiently.
Databricks Named a Leader in the 2025 Gartner® Magic Quadrant™ for Data Science and Machine Learning Platforms: https://www.databricks.com/blog/databricks-named-leader-2025-gartner-magic-quadrant-data-science-and-machine-learning
Build and deploy quality AI agent systems: https://www.databricks.com/product/artificial-intelligence
See all the product announcements from Data + AI Summit: https://www.databricks.com/events/dataaisummit-2025-announcements
Chapters
0:00Introduction1:20The inherited problem3:18The abstraction6:54The problem8:01The solution10:03High accuracy16:00Stateful Routing18:56Lessons Learned20:55Conclusion
FAQs
What is a routing agent and why is it useful?
A routing agent intelligently directs questions to the best AI assistant for each task. This pattern simplifies management of multiple AI systems by providing a single interface while ensuring each question reaches the most appropriate assistant.
How do DSPy optimizers improve routing accuracy?
DSPy includes optimizers that automatically improve routing decisions based on examples. This video demonstrates how these optimizers can maximize the accuracy of route selections while maintaining performance at scale.
How can I reduce latency in my routing agent?
This video covers techniques for optimizing smaller models to reduce latency, enabling faster response times. This includes deploying lighter-weight models through the Databricks Data and AI platform's model serving capabilities.
How do I track performance and ensure security in production?
This video addresses production concerns including stateful interactions, authorized access controls, and real-time performance monitoring to ensure your routing agent operates securely and efficiently at scale.
Full transcript
0:00 Introduction
Okay, thank you everybody for coming. I hope you had a great time at the summit. This is one of the final sessions. Thank you for making it here. We're going to be talking about uh implementing a a routing pattern, a route routing agent leveraging an open source library called DSPI in a databicks platform. So a little bit about me. I'm a staff data scientist at the ML practice team uh in the professional services uh unit of data bricks.
And about a year and a half ago I stumbled into DSPI. I was implementing a text to SQL project and I couldn't make it to have a any any better accuracy. So I read about this this package that will give me some optimizers that I could then apply and it it not only allowed me to improve the accuracy uh but also gave me a very very nice abstraction a very nice way of seeing the problem and prompting that uh I have used ever since.
So I find it very powerful and I'd like to share it with you today to give you an idea of what I mean by that is um prompting has an inherited problem what I call the big wall of text you you start with a very simple task very essential task and it tends to work okay at the beginning so this is a a classification sentiment classification and a very simple phrase will do at the beginning, but then you know you crash with reality and you start seeing examples that don't quite work and you start looking at tricks and you apply role play.
1:20 The inherited problem
You apply background and context and you better detail instructions and you better define your categories. You you give it analysis. You you start using all the tricks that you have in your bag and every day you more and more tricks are are kind of discovered. And these tricks may be good for one language model but not for another one. And very soon you see that your very simple very essential task becomes this big wall of text.
And the main problem you have is in this big wall of text you have conflated all different concerns. you have all these tricks that are basically just um it's accidental complexity that you added to make your prompt better and to get better quality on your results. But this is a kind of a greedy optimization that you did based on the data that you have at hand at the moment in an ad hoc fashion. Maintaining this is a nightmare if you have to change the essential task.
Now you have to start basically from scratch. it's it's um it's not uh very good for the long term. So we had been doing this for you know two years or so. Genai has been the kind of more common in the last two years. So now we're starting to see the pains of maintaining this type of systems and the abstraction that the spine provides you help us with this problem. It's um a very simple abstraction. You start with a signature which is kind of a structure prompt and this uh class Python class that inherited from DSPI signature has an instruction is is your your doc string and you're telling it what to do what's your your main concern and then you define your inputs and by the way you can type them to type inputs you can also give them descriptions in the input field you can find the description and then you define your outputs and it's it's very handy as you can see uh I can define my output with the type literal with the predefined emotions that I want to classify.
3:18 The abstraction
So this is exactly the same that I showed you before with the very um succin sentence but in a programmatic way and now you have defined in an extructor way your main intention and because it's a structure this pie can start exploring changing different things and optimizing it. So it could alter just the instruction. It could uh put more emphasis on extracting the types. It could give few short examples. It could do many things because it's not just a blob.
Now it has some structure. But but it's it's very intuitive for you to apply this this structure in your prompts. So it's not a big lift to move towards uh the spy. A besides defining the signature, you also uh define your kind of prediction strategy which in DSP is called a module. And in DSP offers you out of the box several strategies. You have the predict which is the essential one, the simplest one. It's just a prediction.
You have chain of thoughts. You have react and others. And you can create your own modules by composing these uh out ofthe-box modules. And then finally you define your language model which if you're using data bricks is very simple. You just pointing to the foundation models and that's it. That's all you need to do a DSPI program. And the beautiful thing is that you can change any of these abstractions independently of the other abstractions.
So your signature could remain the same and you can change your prediction strategy to be a chain of thought or a react uh uh strategy. You can use a different language model and DSP is actually under the hood using what is called an adapter to unfold the signature and actually render the prompt that will be sent to the language model. a looking at the language model and uh selecting the adapter that would be most appropriate for that language model.
So it allows you to to plug and play different things and iterate your PC towards production ve very very easily and it's it's it's very simple and intuitive. You don't need any other constructs. It's also very free. It doesn't impose to you like an order or a a DSL that you need to follow the domain specific language or a pipeline that you need to follow. is it's just Python. It almost feels like you're using helper functions in your Python.
That's that's all you have. And at the end, you have your classifier. You pass it and that's that's all you have. So with this in mind, uh I'm going to spend the rest of the session talking about this pattern I have been implemented with several clients leveraging DSPI to route uh requests. And the problem I'm trying to solve with this pattern is uh clients have been again implementing in the last two years or so many AI assistants, many AI solutions but they tend to be dispersed.
6:54 The problem
So different endpoints, different user interfaces and users are starting to get lost. They don't know where to find which assistant governance is all fragmented. If we could just add a single point of entry, a place where everybody is is is sent and that entry point will know which AI solution should be called depending on the request. Then that will allow you to have a consolidated governance and it will make the life of the users easier.
Therefore, the option will be better. So this is the problem that we're trying to solve. Now if you uh close your eyes, squeeze your eyes, uh this will probably smell to you like React. This is like an agent and is reasoning and acting, reasoning and acting and you would be right. I this is where I started. The Spy has a module as I mentioned that is React which basically does a reasoning phase that selects the tool and extracts parameters from the requests.
8:01 The solution
Okay, selects a tool that would that seems to be the best one for answering the request and then extracts the parameters that the tool needs out of the request. Then acts by executing the tool and then finally reasons again to say okay I have do I have enough information now to answer the question or to answer the request. If the answer is yes, then the it renders the response. But if the answer is no, then it loops again and it starts selecting another tool, extracting parameters, running it, and keeps doing this until it has enough information to answer the request.
So this is very very similar to what we need, but it's actually a bit more complex than what we need. We can simplify it because if you see in the router, we don't have a sequence of steps. We have just one step. We need to select which of the available tools is the one that we want. So um I've taken the module that comes and then the spy is is open source you can see it you can modify it and I created then this router pattern which is just a simplification that only does a single reasoning step selects the tool extracts the parameters then it calls the tool and finally it gives you either the response out of the tool or if there is an error which tends to be particularly in the parameter extraction part then it focus on just extracting the parameter and solving that problem.
So it will skip selecting a tool it will just focus on better extracting the parameters. Okay. Now in order for this pattern to be effective there's some fundamental aspects that we need to cover. No it has to high accuracy. It doesn't matter how good is the tool or the solution that is being called. If the router sends it to the wrong one, then you know everything will be bad after that. You want to be able to scale it.
10:03 High accuracy
You want to be able to have tens of tools or hundreds of tools. And you want to be able to have stful interactions. So it could be that the tool needs more information to proceed. uh you're asking where is the best pizza place in the city and the correct tool is selected the pizza place recommener but the tool needs to know where you're located. So it's it asks back to the user hey um where you located. You could think then having the tool as a state machine that transitions to a state of requesting information and when the user sends information you don't need to go through the process of selecting the tool instructing parameters again you know this tool is in this state is in an intermediate state you can jump all that and go and then continue from there okay so for the high accuracy portion we leverage the DSPI uh optimizers optimization particularly a new one that was unveiled yesterday which is Simba.
I won't go in details on Simba but at a high level I can tell you that it uses mini batches. So you give it many examples and it will sample the the examples and apply alterations to the instructions the future examples. It will try many things and select the changes that provide back a bigger impact, a bigger incremental impact. So those changes are are selected and then it samples another mini batch and keeps applying that approach until it either reach the max number of iterations that you give it or it the improvements are below a threshold that you set up.
Okay. Now what's really important in this case is how you measure improvement. The good news is that in the case of uh the router, it's kind of simple because if you can set up the questions ahead of time, you know which tool should be called and you can know which parameters should be uh extracted. So you can set up those expectations ahead of time like I'm doing here. I have a a list of dictionaries with the question and I know that for that question uh the tool uh call should be the the tickets tool and the argument should be this one with this value.
Then it's very easy to create an evaluation function a metric that receives the example and the prediction and just compares them. So it would be true if the tool and the name of the tool and the parameters are the expected ones. It would be false if if if not the way then you use DSPI is is very simple. You transform your list of dictionaries into a DSP by example and instantiate Simba tell passing the evaluation function telling it how many iterations it should take and what's the max number of future examples or demonstrations that you want to add.
Then you call the compile and that will do the optimization. And this is a step that you do ahead of time. This is a step that you do before you deploy the model. This is not a step that you do on runtime. The compiler will run for some time and then give you an optimize agent and that optimize agent that optimized model is the one that you will then register and deploy. On the scalability side, what we can do is we can rank the tools based on the question.
So we could leverage vector search, database vector search to see which tools are more likely to be useful based on the semantic similarity to the question. So this won't select the tool per se, but it will allow you to um diminish the number of tools that you're passing to the router, therefore making the prompt a lot smaller. A vector search is is very fast. So it would be milliseconds and by making the con the prompt or the context window much smaller it would be less costly and it would be faster.
Vector search is built to handle billions of records. So you have a lot of room to add tools when you apply this approach. that's what you pass to the router. Now there is a step you need to take ahead of time. You need to create a table that has all the tools and descriptions of the tools, examples for the tools and have a column that has all these combined and you index that column. Again, as I mentioned, vector search is built for billions of records.
So, if you have tens of records or hundreds of record, that's that's that's little data. Processing this pre-step takes minutes. It's very fast, but you do need to do it. So, on the stateful side, this is one I'm going to give you a bit of a pymic approach and one that I feel is more elegant. But the polemic one for me has been very useful and very convenient. So, so I'm going to share it with you. A many of the consumers of the routers would be uh UI systems they already handle session state.
16:00 Stateful Routing
So you can pass back and forth the state to the consumer and this way you can uh keep the router stateless. The benefit of having the router stateless is then that now you can replicate it and you can scale very easily. You can pass the state in the custom outputs and receive the state in the custom inputs of the chat models and the chat agent MLflow Python classes. Now this is convenient but it's not as elegant and has some drawbacks which are the following.
You are actually coupling the router with the consumer. Now the consumer needs to know a little bit about the internal processings of the routers which is not good. It's kind of a smell. It's convenient. I have used it. I'm guilty. I have used it. But uh use it you know be careful when you use it. Also, you're adding more traffic, network traffic. So, the state has to be like really really small. Uh to give you an example, going back to that example that I mentioned of um the where's the best pizza place?
The tool will say, well, in which city are you located? And it will send back that custom output with the state that has the tool and the tool state. And then the UI when sending back the answer of the user will then send back the state as is the same state in the custom input. Then the router will know to not select a new tool but keep using that tool in the in the state that it was before. Now you can do all that in a much better way using leg base.
Now caveat leg base is really news. was unveiled yesterday but it will be a much better approach. It is uh built for scalability uh low latency for both storage and retrieval. It's a managed solution so it's a lot simpler to implement it. You have no coupling no logical coupling between the the service the router and the consumer the client and it's all open source uh based technology. So, so I would highly recommend you you can use a very similar approach of storing the state the very same state but instead of passing it back and forth you could use lake base for that.
18:56 Lessons Learned
Er so some of the lessons learned by implementing this pattern with customers first um premature optimizations are cause of many headaches. I wouldn't start with the DSPI optimization right away. You can get very far by just using DSPI at the beginning. You're staging it using your signature making everything very very clean and you you get very good accuracy. you set up your your um metric, your evaluation function, you can use all that and only when you have critical mass, when you have enough tools, when you have also enough representative data, then you can start using the optimizers.
It's very important to have a really good delineation of the responsibilities. If you have tools that um the description overlap semantically, the language model will get confused. It will select one or the other depending. So you need to spend some good time and this is non-trivial when you start having many many many tools. So it's it's very very important to delineate the responsibilities very well. And then finally for tracing and troubleshooting MLflow traces is is your friend.
It's is really good really powerful. If you put that line of code that I have there h mlflow. dsspi. autolog Auto log you got full logging of all the traces of everything that's happening in DSP and you can see how DSP is transforming the signature into the prompt that is actually sent to the language model what the language model is sending back how it's parsing it out how it's calling the tool and everything is is set up here in a very nice user interface and by the way if you're using model serving this and you activate the spy autolock this will go to the inference table so you can see what happened in production later on.
So this is very very powerful. Well, in conclusion, I you know hope that um I make you curious about DSPI. DSP3 was unveiled yesterday is production ready. This pattern that I'm showing you I have used in production. We have clients using them right now in production and we have you know volume tested and so we've seen thousands of user using the pattern. I hope you find of interest the router pattern and you see this the the problem that it helps solving and how it's a a modular scalable approach for solving that model and stating a little bit the obvious uh data bricks is a is a very good platform for implementing these type of systems because it gives you out of the box governance and uh monitoring and much of what you Okay.
20:55 Conclusion
So, with that in mind, I think I have about uh two minutes or so for any questions that you may have.
Learn more about the Databricks Data and AI platform.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.