---
title: "Route an LLM workflow with C# predicates"
url: https://recursiveai.org/onellm/cookbook/choose-agent-branch-with-csharp/
description: "Route an LLM workflow with a C# predicate. Use a typed OneLLM decider and a scripted local provider to test both routes without an API key."
version: "1.0.1"
---

# Route an LLM workflow with C# predicates

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/choose-agent-branch-with-csharp/choose-agent-branch-with-csharp.zip)

Use a Boolean rule in C# to choose the next step from a model’s answer.

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

Put this predicate in a OneLLM decider’s `Decide` method.

```csharp
// Probability that this is about billing.
PrintRoute("Duplicate charge", 0.95);
PrintRoute("Where is my parcel?", 0.20);

static void PrintRoute(
    string ticket, double probability) =>
    Console.WriteLine(
        $"{ticket} -> {Route(probability)}");

static string Route(double probability) =>
    probability >= 0.9
        ? "billing" : "review";
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/choose-agent-branch-with-csharp/choose-agent-branch-with-csharp.zip), then `dotnet run`.

```text
Duplicate charge -> billing
Where is my parcel? -> review
```

## The full program

A OneLLM [decider](https://recursiveai.org/onellm/docs/deciders/) asks a typed question and passes its answer to your `Decide` method. The local provider supplies the probabilities instead of making a cloud call. The rule returns a route name for your application to use; this example prints it rather than starting another workflow.

`route.decider.yaml` in the download defines the input and question. The `noul` type asks a yes/no question and returns a probability for “yes.”

```yaml
model: openai-decisions
state:
  ticket: "{{ .inputs.ticket }}"
questions:
  billing:
    type: noul
    instructions: Is this ticket about billing?
```

The shipped catalog resolves `openai-decisions` to `gpt-6-luna-decisions` under `openai`, where this program registers the scripted provider.

The package supplies the generator. Include the YAML as a build input so `[Decider]` can generate `Answers` and the `Decider` property on the partial class:

```xml
<ItemGroup>
  <AdditionalFiles Include="route.decider.yaml" />
</ItemGroup>
```

```csharp
using System.Text.Json;
using System.Text.Json.Nodes;
using OneLlm;
using OneLlm.Deciders;
using OneLlm.Providers;
using OneLlm.Testing;

var runtime = new Runtime();
var provider = new ScriptedDecisionProvider("openai");
runtime.Providers.Set("openai", provider);

await Check("Duplicate charge", 0.95, "billing");
await Check("Where is my parcel?", 0.20, "review");

async Task Check(string ticket, double probability, string expected)
{
    provider.Responder = _ => new DecisionResponse
    {
        Answers = new JsonObject
        {
            ["billing"] = new JsonObject
            {
                ["type"] = "noul", ["noul"] = probability
            }
        }
    };
    var result = await runtime.DecideAsync(RouteTicket.Decider,
        new Dictionary<string, object?> { ["ticket"] = ticket });
    string? route = result.Outputs.GetString();
    if (route != expected)
        throw new InvalidOperationException($"Expected {expected}, got {route}.");
    Console.WriteLine($"{ticket} -> {route}");
}

[Decider("route.decider.yaml")]
static partial class RouteTicket
{
    static JsonElement Decide(Answers answers, JsonElement inputs)
    {
        bool highConfidence = answers.Billing.Probability >= 0.9;
        return JsonSerializer.SerializeToElement(
            highConfidence ? "billing" : "review");
    }
}
```

```text
Duplicate charge -> billing
Where is my parcel? -> review
```

If the caller omits `ticket`, rendering the decider’s state fails on the missing name before the question is sent.

## Variations

**Check the boundary.** Add these lines before `Check`’s local function in the full program. A score equal to `0.9` is included; one just below it goes to review.

```csharp
await Check("At the cutoff", 0.90, "billing");
await Check("Below the cutoff", 0.89, "review");
```

## Related recipes

- [Classify support tickets with an LLM in C#](https://recursiveai.org/onellm/cookbook/classify-support-tickets/)
- [Test LLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)

## The short answer

Put the routing rule inside the C# decider’s `Decide(Answers, JsonElement)` method and read the returned route from `DecisionResult.Outputs`. Here the YAML asks a yes/no probability question (`noul`), the generator exposes its result as `answers.Billing`, and a Boolean comparison picks `billing` or `review`. The provider supplies the probability, but C# sets the threshold and decides what counts as a match.

## How do you add conditional routing to an LLM workflow?

Separate interpretation from routing. The decider turns an unstructured ticket into a typed probability of a billing issue. Your predicate turns that value into one of the routes your application actually handles. Below the cutoff, a ticket goes to review; the rule does not label every low score as shipping. The same ticket and model answer can be routed differently later by changing the rule, without changing the question in the YAML.

If the application already has a trusted billing flag, route on that flag directly rather than asking a model to infer it again. Use the decider when you need an answer about text or other state, then apply the known rule to the answer and, if necessary, the original inputs. The `inputs` parameter in `Decide` is available for that distinction.

## How do you test both workflow branches without a model?

Use `ScriptedDecisionProvider` to return one answer above the threshold, one below, and one equal to it. Assert the returned route, not just that the decider completed. The fixtures check the C# rule and the mapping from a scripted answer into generated `Answers`. They do not show that the threshold is suitable for real tickets or that a live model will give the example probabilities. Test the routing rule with controlled values before separately evaluating the question on real data.
