---
title: "Include another file in a prompt template in C#"
url: https://recursiveai.org/onellm/cookbook/include-reusable-file-in-prompt-template/
description: "Include another file in a C# prompt template with OneLLM. Render both files with the same variables, resolve relative paths, and diagnose include cycles offline."
version: "1.0.1"
---

# Include another file in a prompt template 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/include-reusable-file-in-prompt-template/include-reusable-file-in-prompt-template.zip)

Put `{{ embed "parts/tone.txt" }}` in your prompt. Both files use the same variables; no model runs.

```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 file = Path.GetFullPath("main.txt");
var template = Template.Parse(
    File.ReadAllText(file));
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
Console.WriteLine(template.Render(scope, new()
{
    RootKey = "file:" + file,
    EmbedSources = [new FileEmbedSource(".")]
}).TrimEnd());
```

`main.txt`:

```text
Write a reply for {{ .name }}.
{{ embed "parts/tone.txt" }}
```

`parts/tone.txt`:

```text
Keep the reply to {{ .name }} brief.
```

Both sample files are in the zip. Both lines render with `name` set to `Ada`. `FileEmbedSource(".")` is fine when your working directory is the folder you want to allow includes from.

Unzip [the example](https://recursiveai.org/onellm/cookbook/include-reusable-file-in-prompt-template/include-reusable-file-in-prompt-template.zip), then `dotnet run`.

```text
Write a reply for Ada.
Keep the reply to Ada brief.
```

## The full program

The full program sets the allowed include root to the parent folder of `main.txt` and gives the template the full file key so OneLLM can identify the entry file during nested renders.

Run from the folder containing `main.txt`.

```csharp
using OneLlm.Templating;

var file = Path.GetFullPath("main.txt");
var root = Path.GetDirectoryName(file)!;
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
var options = new TemplateRenderOptions
{
    RootKey = "file:" + file,
    EmbedSources = [new FileEmbedSource(root)]
};
var prompt = File.ReadAllText(file);
Console.WriteLine(
    Template.Parse(prompt).Render(scope, options).TrimEnd());
```

```text
Write a reply for Ada.
Keep the reply to Ada brief.
```

A missing include throws `TemplateException` with kind `Embed` and the location of the failed call.

## Variations

To check a circular include, add this second line to `parts/tone.txt`:

```text
{{ embed "../main.txt" }}
```

Replace the final `Console.WriteLine` in the full program with this handler:

```csharp
try
{
    Template.Parse(prompt).Render(scope, options);
}
catch (TemplateException error)
{
    Console.WriteLine($"{error.Kind}: {error.Line}:{error.Column}");
    Console.WriteLine(error.Message);
}
```

The first line is `Embed: 2:4`, the outer call in `main.txt`. The message identifies the nested call in `parts/tone.txt` and reports `cycle detected`, with the full file keys for `main.txt → parts/tone.txt → main.txt`. Remove the include back to `main.txt` to break the cycle.

## Related recipes

- [Render prompt variables and conditionals 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

Use `{{ embed "parts/tone.txt" }}` in the main template and pass a `FileEmbedSource` in `TemplateRenderOptions.EmbedSources`. Give the main template a `RootKey` made from `file:` and its absolute path. `embed` inserts the result of rendering the second file; it doesn’t paste that file as raw text. In the example, both files read `.name` from the same dictionary, so changing `Ada` in C# changes both lines. The standalone renderer produces a string and does not send it anywhere.

## How do relative paths work in a prompt template include?

`FileEmbedSource` resolves paths relative to the file containing the `embed` call. The main file includes `parts/tone.txt` relative to its own folder, but a call inside `tone.txt` starts in `parts/`. That distinction matters when you move a shared template into a subfolder: its own includes move with it and are still evaluated relative to the new location. `RootKey` gives the initial render that same file identity.

The source’s root is a separate setting: it controls which files can be read even when a relative path uses `..`. For example, from `parts/tone.txt`, `../main.txt` resolves inside the configured root; its problem here is the cycle, not the directory traversal. `FileEmbedSource` ignores absolute include references and checks the resolved path after following symbolic links. OneLLM configures no embed sources by default. If you supply more than one, the first source to return content wins.

## How do you fix a circular reference in a template include?

Read the chain in the exception and look for the first repeated file. In the example, `main.txt` includes `tone.txt`, which tries to render `main.txt` again. A file cannot embed itself, even indirectly. Keep shared text in a file that does not depend on the prompts that include it, or extract the common text into a third file and include that from both places.

A long chain with no repeated file can still hit `MaxEmbedDepth`, which defaults to 16. That error also identifies the include call. Raising the depth can allow a deliberately deep tree, but it does not fix a cycle.
