---
title: "Add a custom prompt template function in C#"
url: https://recursiveai.org/onellm/cookbook/register-custom-prompt-template-function/
description: "Add a custom function to a C# prompt template. Register a OneLLM helper before parsing and render a formatted ticket ID without calling a model."
version: "1.0.1"
---

# Add a custom prompt template function in C#

**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/register-custom-prompt-template-function/register-custom-prompt-template-function.zip)

Register a C# helper to format values as the prompt renders. No model needed.

```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.

`{{ ticket .id }}` calls `ticket` with the `id` value; the function name and its arguments are separated by spaces.

```csharp
using OneLlm.Templating;

var functions = new TemplateFunctions()
    .Add("ticket", id => $"SUP-{id}");
var parse = new TemplateParseOptions
    { Functions = functions };
var values = new Dictionary<string, object?>
    { ["id"] = "42" };
const string prompt =
    "Summarize {{ ticket .id }}.";
Console.WriteLine(
    Template.Render(prompt, values, parse));
```

If `ticket` isn’t in the function registry passed at parse time, `TemplateException` reports an undefined function.

Unzip [the example](https://recursiveai.org/onellm/cookbook/register-custom-prompt-template-function/register-custom-prompt-template-function.zip), then `dotnet run`.

```text
Summarize SUP-42.
```

## Variations

**Pass the ID through a pipeline.** Replace the `prompt` constant with this equivalent template, where the left side becomes the function’s argument:

```csharp
const string prompt =
    "Summarize {{ .id | ticket }}.";
```

## Related recipes

- [Render a prompt template in C#](https://recursiveai.org/onellm/cookbook/render-prompt-template/)
- [Test prompt template conditionals in C#](https://recursiveai.org/onellm/cookbook/test-prompt-template-branches/)

## The short answer

Create a `TemplateFunctions` registry, add a named C# delegate, and pass that registry in `TemplateParseOptions.Functions`. The action `{{ ticket .id }}` calls `ticket` with the scope’s `id` value. Here `Add` uses the overload for a single string argument. The delegate returns `SUP-42` for input `42`, and the renderer inserts that string into the prompt. It doesn’t ask a model to format the ID.

## How do you pass arguments to a custom template function?

Use spaces between the function name and its arguments, not C# call syntax. A dot before `id` tells the template to look up that field in the current scope. In a pipeline, the prior result becomes the last argument of the next call; with only one argument, `{{ .id | ticket }}` and `{{ ticket .id }}` mean the same thing.

Match the delegate’s argument kind to the data you supply. Here the ID is the string `"42"`; the one-string `Add` overload declares `ArgKind.Text`. For a helper that takes a number or Boolean, the [function reference](https://recursiveai.org/onellm/docs/templating/#onellm-only-extensions) describes declaring argument kinds explicitly with the `TemplateFunction` API. Keep formatting in the delegate and leave input lookup in the template so the same formatter can process IDs from different fields.

## Why is my custom function undefined in the template?

The parser needs to know the function name before it can accept the action. Pass the registry you added `ticket` to as a parse option, and check that you used the same spelling and case in the template. If you use `Template.Parse` separately so you can render with different input maps, pass the registry at that call; changing the input dictionary can’t register a new function. Finish adding functions before rendering. A new registry starts with the standard helpers, but adding an existing name replaces that function for templates that use your registry. Use a distinct name when you want to keep the original behavior.
