> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompt Management

> Create, save, version, and use prompts in TrueFoundry's AI Gateway.

TrueFoundry's Prompt Management lets you create, save, version, and reuse prompts in a single place — the **Prompt Registry**. Define your system message, user message, input variables, guardrails, and structured output once, then run those prompts from your apps via the AI Gateway or SDK.

> [!NOTE]
>
> Prompts are stored inside **Repositories**. Access to create, view, edit, or use a prompt is governed by the access control on its parent Repository, so you do not need to configure separate permissions for each prompt. See [Repositories](https://www.truefoundry.com/docs/platform/repositories.md) for the shared repository concept, storage backing, and access control.

## Creating a Prompt

**1. Open the Prompt Registry**

Go to **Prompts** in the left navigation and click **+ Create Prompt** in the top right.

![Prompt Registry showing the list of saved prompts with the Create Prompt button highlighted in the top right](https://mintcdn.com/truefoundry/xnxwG9wbAPzCd_DD/images/docs/ai-gateway/prompts-create-button.png?fit=max\&auto=format\&n=xnxwG9wbAPzCd_DD\&q=85\&s=55dc4dad255b892b8c8ec49e1c128e6f)

**2. Configure the Prompt**

The **New Prompt** screen opens with a chat-style editor. Configure your prompt by:

* **Choosing a model** from the model selector (supports virtual models for load balancing and fallback)
* **Editing the System prompt** to define the AI's role and behavior
* **Adding a User message** to provide the input or query
* *(Optional)* Configuring **Logging Config**, **Cache Config**, **Input/Output Guardrails**, **Structured Output**, and **Metadata** from the left panel

![New Prompt screen showing model selector, system prompt input, user message field, and the left configuration panel with Logging Config, Cache Config, Input Variables, Guardrails, Structured Output, and Metadata options](https://mintcdn.com/truefoundry/xnxwG9wbAPzCd_DD/images/docs/ai-gateway/prompt-create.png?fit=max\&auto=format\&n=xnxwG9wbAPzCd_DD\&q=85\&s=a82f22ef7611d3e43840c704f0d2f7b1)

**3. Add Input Variables (Optional)**

Make prompts reusable by referencing template variables with the `{{variable_name}}` syntax inside your messages. Variables are auto-detected and appear under **Input Variables** in the left panel, where you can preview them with sample values before saving.

![New Prompt screen with a system message about providing capital and population, a user message containing the \{country} variable, and the Input Variables panel showing the auto-detected country variable](https://mintcdn.com/truefoundry/xnxwG9wbAPzCd_DD/images/docs/ai-gateway/prompt-add-variables-and-save.png?fit=max\&auto=format\&n=xnxwG9wbAPzCd_DD\&q=85\&s=fc8059aad6cca4ca68ba32c2df357fb3)

**4. Save the Prompt**

When you're happy with the prompt, click **Save New Prompt** in the top right. A side panel opens — fill in:

* **Repository** — Select an existing repository where the prompt will be saved. Repository access controls who can view, edit, and use the prompt. See [Repositories](https://www.truefoundry.com/docs/platform/repositories.md).
* **Prompt Name** — Give your prompt a descriptive name
* **Commit message** *(optional)* — Brief description of the change
* **Store selected model in prompt template** — Keep this enabled if you want the prompt to remember its chosen model

![Save Prompt Template side panel with Repository dropdown set to kunwar-local, Prompt Name set to test-prompt, an optional commit message, and the checkbox to store model information in the template](https://mintcdn.com/truefoundry/8pZwxSWNXs6h_9YO/images/docs/ai-gateway/prompt-enter-saving-data.png?fit=max\&auto=format\&n=8pZwxSWNXs6h_9YO\&q=85\&s=16fb1a5d0594a00bae332c36dffe4751)

## Viewing Your Prompt

Once saved, you land on the prompt detail page at `Prompt Registry > {repository} > {prompt-name} > v{version}`. From here you can:

* **Prompt / Spec** — View the prompt content and its full configuration
* **Try Prompt** — Open the prompt in a runnable view to test it interactively
* **Edit v\<N>** — Make changes; saving produces a new version
* **Version History** — Browse and diff previous versions
* **FQN** — Copy the prompt-version FQN (needed when calling the prompt from code)
* **Use Via SDK / Use Via AI Gateway** — Get ready-made code snippets for using the prompt in production

![Prompt detail view showing prompt name test-prompt at version v1, model, configuration panel, system prompt, user message with the country variable, and top-right actions: FQN, Try Prompt, Version History, Edit v1](https://mintcdn.com/truefoundry/8pZwxSWNXs6h_9YO/images/docs/ai-gateway/prompt-view.png?fit=max\&auto=format\&n=8pZwxSWNXs6h_9YO\&q=85\&s=27754de87f24a88a9e76dac99ae9c502)

## Advanced Configuration

Each prompt template can be configured with the same controls as a regular Gateway request:

> [accordion] Input Variables — dynamic content
>
> Reference variables in system or user messages using `{{variable_name}}` syntax. Pass values at runtime via `prompt_variables` (Gateway) or as `variables` to `render_prompt` (SDK). See [Using the Prompt](#using-the-prompt) below.

> [accordion] Guardrails — input and output policies
>
> Attach **Input Guardrails** and **Output Guardrails** to enforce safety, PII redaction, content moderation, or custom checks on every run of the saved prompt. Learn more: [Guardrails Overview](https://www.truefoundry.com/docs/ai-gateway/guardrails-overview.md).

> [accordion] Virtual Models — load balancing and fallback
>
> Bind the prompt to a [Virtual Model](https://www.truefoundry.com/docs/ai-gateway/virtual-model.md) to get load balancing, fallback, and retries across multiple gateway targets when running the saved prompt.

> [accordion] Structured Output — JSON schema
>
> Force the model to respond in a specific JSON schema. Useful when downstream code needs to parse the response programmatically.

## Versioning

TrueFoundry automatically creates a new version every time you edit and save a prompt. Versioning lets you:

* Roll forward and roll back across versions
* Compare exact changes between any two versions
* Pin specific versions to specific apps or environments

> [accordion] Edit and Create a New Version
>
> Open any prompt and click **Edit v\<N>** to make changes. Saving the edited prompt produces a new version (e.g. `v2`).

> [accordion] Browse Version History
>
> Click **Version History** on the prompt detail page to see all versions of the prompt with their commit messages and authors.

> [accordion] Compare Versions (Diff Mode)
>
> Inside Version History, switch to the **Version Difference** tab to see exactly what changed between two versions in a GitHub-style diff view.
>
> ![Version Difference tab showing a side-by-side diff between two prompt versions](https://mintcdn.com/truefoundry/YDqmXEab_DxBZe5e/images/prompt-diff-mode.png?fit=max\&auto=format\&n=YDqmXEab_DxBZe5e\&q=85\&s=f756246bfbb2e20e67b3b2e8b5065a9c)

## Using the Prompt

There are two ways to consume a saved prompt from your application:

| Method | When to use |
| - | - |
| **Use Via AI Gateway** *(server-side rendering)* | The AI Gateway renders the prompt and runs it — simplest setup, no extra dependencies. |
| **Use Via SDK** *(client-side rendering)* | Fetch the template and render it locally — full control over message construction. |

In both cases you need the prompt version's **FQN**, which you can copy from the **FQN** button on the prompt detail page.

> [tab] Use Via AI Gateway (server-side rendering)
>
> Pass the prompt version FQN in the request body. The AI Gateway renders the prompt and executes the call.
>
> **Parameters**
>
> * `prompt_version_fqn` — The fully qualified name of your prompt version
>
> * `prompt_variables` *(optional)* — Variables to substitute into the prompt template
>
> > [!NOTE]
> >
> > **Important considerations**
> >
> > * If the prompt version doesn't have a model configured, pass the model in the request body via the `model` parameter
> > * If you specify a model in the request body and the prompt version already has one configured, the request body model takes precedence
> > * Any `messages` passed in the request body are appended to the messages defined in the prompt version
>
> Open the **Use Via AI Gateway** tab on the prompt detail page to get a ready-to-paste snippet in your preferred client library (OpenAI, LangChain, Node.js, Curl, REST, and more).
>
> ![Use Via AI Gateway tab showing Python OpenAI client code calling client.chat.completions.create with prompt\_version\_fqn in extra\_body, alongside a left-side library picker including OpenAI, LangChain, Node.js, Curl, REST, and more](https://mintcdn.com/truefoundry/xnxwG9wbAPzCd_DD/images/docs/ai-gateway/prompt-use-via-ai-gateway.png?fit=max\&auto=format\&n=xnxwG9wbAPzCd_DD\&q=85\&s=ab19db7b6e71582d2eb23b99a8a1f90d)
>
> ```python theme={"dark"}
> from openai import OpenAI
>
> client = OpenAI(
>     api_key="your-tfy-api-key",
>     base_url="{GATEWAY_BASE_URL}"
> )
>
> stream = client.chat.completions.create(
>     messages=[],
>     model="",
>     stream=True,
>     extra_headers={
>         "X-TFY-METADATA": '{"your_custom_key":"your_custom_value"}',
>         "X-TFY-LOGGING-CONFIG": '{"enabled": true}',
>     },
>     extra_body={
>         "prompt_version_fqn": "chat_prompt:truefoundry/default/my-second-prompt:1",
>         "prompt_variables": {
>             "name": "John Doe",
>             "age": "30"
>         }
>     },
> )
>
> for chunk in stream:
>     if (
>         chunk.choices
>         and len(chunk.choices) > 0
>         and chunk.choices[0].delta.content is not None
>     ):
>         print(chunk.choices[0].delta.content, end="", flush=True)
> ```

> [tab] Use Via SDK (client-side rendering)
>
> Fetch the prompt template, render it locally, then call any OpenAI-compatible client.
>
> Open the **Use Via SDK** tab on the prompt detail page to copy the snippet pre-filled with your prompt version FQN.
>
> ![Use Via SDK tab showing Python code that uses truefoundry.client.prompt\_versions.get\_by\_fqn to fetch the prompt template, renders it with render\_prompt, and sends the result to OpenAI().chat.completions.create](https://mintcdn.com/truefoundry/8pZwxSWNXs6h_9YO/images/docs/ai-gateway/prompt-use-via-sdk.png?fit=max\&auto=format\&n=8pZwxSWNXs6h_9YO\&q=85\&s=13d3aba70178cc3dae32fddbe032d3e5)
>
> ```python theme={"dark"}
> from truefoundry import client, render_prompt
> from openai import OpenAI
>
> # Fetch the prompt template
> prompt_template = client.prompt_versions.get_by_fqn(
>     fqn="chat_prompt:truefoundry/default/my-second-prompt:1",
> ).data.manifest
>
> # Render the prompt with input variables
> variables = {}  # Add your variables here
> prompt = render_prompt(prompt_template, variables=variables)
>
> # Send request to OpenAI directly
> response = OpenAI().chat.completions.create(
>     messages=prompt["messages"],
>     model=prompt.get("model") or "gpt-4o-mini",
>     **(prompt.get("parameters") or {}),
> )
>
> print(response.choices[0].message.content)
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.