Routing by Request Content with Step Functions
This lesson shows how to build content-based routing with Step Functions, which classifies each request and sends it to a specialised model or workflow.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a classifier for requests.
- Branch to specialised models with a Choice state.
- Measure routing quality in both directions.
Classifying the request
Content-based routing begins by determining each request's category, complexity or required capability. Several classifiers are possible. A small, fast FM prompted to return one label from a fixed list is cheap and flexible, and adapts easily to new categories. Amazon Comprehend custom classification is fast and consistent for a fixed set of categories once trained on labelled examples. Embedding similarity compares the request with example requests for each route and chooses the closest. Simple rules, such as detecting the language, the presence of code or an attachment, need no model at all.
Whatever the method, the classifier must be much cheaper and faster than the models it routes between; otherwise, routing adds more cost and latency than it saves. In Step Functions, the classification step's output feeds a Choice state that sends the request to the appropriate model.
Routing in Step Functions
In Step Functions, a Task state runs the classifier and a Choice state branches on its output to Task states that call specialised FMs: a code model for programming questions, a multimodal model when an image is attached, a reasoning model for complex analysis, a small model for simple questions. Each branch can also run a different workflow, for example RAG for policy questions and an agent for account actions. A Default branch handles unexpected labels.
"Route": {
"Type": "Choice",
"Choices": [
{"Variable": "$.route.label", "StringEquals": "code", "Next": "CodeModel"},
{"Variable": "$.route.label", "StringEquals": "image", "Next": "MultimodalModel"},
{"Variable": "$.route.label", "StringEquals": "complex", "Next": "ReasoningModel"}
],
"Default": "SmallModel"
}Express workflows suit synchronous, high-volume routing with low latency; Standard workflows suit longer, auditable processes. The same logic can live in a Lambda function when Step Functions' visibility is not needed.
The following diagram shows the classifier and the routes.

Content-based routing
Measuring routing quality
Routing errors are costly in both directions. A complex request sent to a small model produces a poor answer, and a simple request sent to a large model wastes money. Routing therefore needs its own measurement.
A labelled set of representative requests, each marked with the route it should take, measures the classifier's accuracy before release. In production, metrics track the share of traffic on each route, quality per route, for example from user ratings or an LLM judge, and the rate of escalations, where a response from a smaller model had to be redone by a larger one. A rising escalation rate or falling quality on one route indicates that the classifier's thresholds or categories need adjusting. The following table compares the classifier options.
| Classifier | Strength | Weakness |
|---|---|---|
| Small FM with label prompt | Flexible; no training | Per-request model cost |
| Comprehend custom classification | Fast, consistent, cheap per request | Needs training data and an endpoint |
| Embedding similarity to examples | No training; easy to extend | Needs good examples per route |
| Rules | Free and deterministic | Only for obvious signals |
Measuring the router
Mixed developer questions
An internal developer assistant receives general questions, code-review requests with pasted code and architecture diagrams as images. One large multimodal model handles everything at high cost.
A Step Functions Express workflow with a small FM classifier and a Choice state sends general questions to a small model, code to a code-strong model and images to the multimodal model. Cost falls, and per-route quality is monitored.
What must every Choice state used for routing include?
Show answer
A Default route, so that unexpected classifications are still handled.
Exam Tips
Remember for the exam
- Classify requests with a cheap, fast classifier.
- Step Functions Choice state routes to specialised models or workflows.
- Always include a Default route.
- Measure classifier accuracy and per-route quality.