---
title: "Escape double curly braces in a C# prompt template"
url: https://recursiveai.org/onellm/cookbook/escape-literal-prompt-delimiters/
description: "Print literal double curly braces with OneLLM’s raw function while still substituting values in the rest of the prompt. No model call or API key required."
version: "1.0.1"
---

# Escape double curly braces in a C# prompt template

**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/escape-literal-prompt-delimiters/escape-literal-prompt-delimiters.zip)

In a OneLLM prompt, `{{ raw "{{" }}` prints two opening braces without stopping substitutions.

In a regular C# interpolated string (`$"..."`) or a `string.Format` format string, write `{{` for `{`; in a OneLLM prompt template, use `raw`.

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

```csharp
using OneLlm.Templating;

var prompt = """
    Example: {{ raw "{{" }} name }}
    Hello {{ .name }}!
    """;
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
Console.WriteLine(Template.Render(prompt, scope));
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/escape-literal-prompt-delimiters/escape-literal-prompt-delimiters.zip), then `dotnet run`.

```text
Example: {{ name }}
Hello Ada!
```

## The full program

```csharp
using OneLlm.Templating;

var prompt = """
    Example: {{ raw "{{" }} name }}
    Hello {{ .name }}!
    """;
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
Console.WriteLine(Template.Render(prompt, scope));

// Markdown code spans already preserve delimiters.
var markdown = "Show `{{ .name }}` to {{ .name }}.";
Console.WriteLine(Template.Render(markdown, scope));
```

```text
Example: {{ name }}
Hello Ada!
Show `{{ .name }}` to Ada.
```

An action is an instruction between `{{` and `}}`. The first action prints the opening braces as text; the second reads `Ada` from `scope`, the values passed into the template. The last line shows that a Markdown code region (inline code or a fenced code block) preserves braces without a `raw` call.

An unmatched opening delimiter outside a code region can cause `TemplateException`; its position refers to the original prompt text.

## Variations

**Print the entire example action literally.** Replace the `Example:` line inside `prompt`:

```text
Example: {{ raw "{{ name }}" }}
```

```text
Example: {{ name }}
Hello Ada!
```

## Related recipes

- [Render a prompt template in C#](https://recursiveai.org/onellm/cookbook/render-prompt-template/)
- [Fail a prompt template on a missing key](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/)

## The short answer

Write `{{ raw "{{" }}` in a OneLLM prompt to print two opening curly braces. The `raw` function returns its string unchanged. Text such as ` name }}` after that action is literal; an action such as `{{ .name }}` elsewhere in the same prompt still reads the `name` value from the scope. Nothing needs to call a model: `Template.Render` takes the template and its values and returns a string.

## How do you escape curly braces in a C# string without changing the template?

There are two readers here: C# reads the string literal, then OneLLM reads the resulting prompt. Use a non-interpolated string for the prompt. The example uses a C# raw string (triple quotes with no `$`) so the double quotes in `raw` need no C# escaping. A regular C# string can hold the same prompt if you escape the inner quotes. Neither approach changes what OneLLM treats as an action. Doubling braces is a [C# formatting rule](https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting#escaping-braces), not how you escape an opening delimiter in a OneLLM prompt.

For a prompt that includes a placeholder example in prose, wrap the example in a single `raw` action as in the variation. When the literal part is just an opening delimiter, pass only that delimiter. There’s no need to replace every brace in the prompt: doing so would also change the actions you want to run.

## How do you keep double curly braces literal inside Markdown code?

[OneLLM’s template engine](https://recursiveai.org/onellm/docs/templating/#code-regions) preserves braces inside Markdown inline code and fenced code blocks by default. If the prompt already uses code formatting to show an example, leave the braces alone in that region. In the full program, the `name` action inside backticks remains visible as template syntax; the one after `to` is evaluated. The backticks remain in the output.

Use `raw` outside code regions, where OneLLM reads delimiters normally. Conversely, if you expect a substitution inside a code example and get the literal action instead, check whether backticks surround it. The parser option `EscapeCodeRegions` can turn off the default behavior for a template that needs substitutions inside code regions.
