A small terminal chat app that shows content-based routing and failover in
Microsoft.Extensions.AI, using
Spectre.Console for the UI.
The models are simulated, so it runs with no API key — and you can take one down mid-conversation to watch failover pick up.
This sample applies the routing APIs developed in
dotnet/extensions#7662 and refined in
dotnet/extensions#7685.
you> why does my code throw a null reference exception
route: code ok
[code] I handle programming questions. ...
you> /kill code
code is down
you> why does my code throw a null reference exception
route: code x -> general ok
[general] I am the catch-all backstop. ...
dotnet runWith no configuration the models are simulated, so it starts instantly and you can force outages.
Set an API key and the same pipeline routes to real models:
# PowerShell
$env:OPENAI_API_KEY = "sk-..."
dotnet run
# bash
export OPENAI_API_KEY=sk-...
dotnet run| variable | default | |
|---|---|---|
OPENAI_API_KEY |
— | set it to go live; unset means simulated |
OPENAI_CHAT_MODEL |
gpt-4o-mini |
model for any route without an override |
OPENAI_EMBEDDING_MODEL |
text-embedding-3-small |
model that decides where a message routes |
OPENAI_CHAT_MODEL_<ROUTE> |
— | model for one route |
OPENAI_REASONING_<ROUTE> |
— | none, low, medium, or high |
OPENAI_TEMPERATURE_<ROUTE> |
— | e.g. 0.8 |
<ROUTE> is CODE, CREATIVE, MATH, or GENERAL. Whatever you set shows up in the tree the
app prints at startup, so you can confirm it took effect.
Per-route settings are where routing earns its keep — send hard problems to a stronger model and everything else to a cheap one:
export OPENAI_CHAT_MODEL=gpt-5.4 # default for every route
export OPENAI_CHAT_MODEL_CODE=gpt-5.5 # code gets the stronger model...
export OPENAI_REASONING_CODE=high # ...thinking hard
export OPENAI_CHAT_MODEL_MATH=gpt-5.5 # math too...
export OPENAI_REASONING_MATH=medium # ...but less of it
export OPENAI_TEMPERATURE_CREATIVE=0.8 # creative gets more varietyrun-gpt5.ps1 and run-gpt5.sh in this repo apply exactly that mix:
$env:OPENAI_API_KEY = "sk-..."
. .\run-gpt5.ps1
dotnet runexport OPENAI_API_KEY=sk-...
source ./run-gpt5.sh
dotnet runDot-source or source them so the variables survive into dotnet run.
These per-route settings are applied with ConfigureOptions on the route's own client, not by the
router — routing decides which client to invoke, and the client carries its own configuration.
/kill still works in live mode: RouteChatClient wraps each route and fails before calling the
real model, so you can demonstrate failover without an actual outage.
Then try:
why does my code throw a null reference exception |
routes to code |
write me a short poem about the sea |
routes to creative |
what is the derivative of x squared |
routes to math |
hey, how's it going |
scores below the threshold, so general |
/kill code then ask a coding question again |
code fails, failover reselects general |
Commands: /kill <model>, /revive <model>, /status, /reset, /help, /quit.
OrderedFailoverChatClient
├── SemanticRoutingChatClient picks a specialist by content
│ ├── code programming
│ ├── creative writing
│ ├── math calculation
│ └── general default, when no profile scores high enough
└── general backstop, when the specialist fails
Both are plain IChatClient implementations, so they nest: the semantic router is just another
client from the failover client's point of view. SemanticRoutingChatClient embeds the last user
message, compares it against the example utterances registered for each specialist, and picks the
best match above scoreThreshold. If that specialist then throws before producing output,
OrderedFailoverChatClient moves to the next entry in its list.
Program.cs |
pipeline wiring and the Spectre.Console UI |
RouteChatClient.cs |
names a route, records invocations, and can be forced to fail |
SimulatedChatClient.cs |
a fake model that streams a canned reply, used when no API key is set |
KeywordEmbeddingGenerator.cs |
an offline IEmbeddingGenerator so semantic routing works without a network call |
The routing types ship in Microsoft.Extensions.AI 10.9.0, so dotnet run restores them from
NuGet.org like any other package.
These types are [Experimental] under diagnostic ID MEAI001, which RoutingChat.csproj
suppresses with <NoWarn>.