---
title: Can I dynamically select sub-prompts at runtime?
description: Learn how to conditionally choose which sub-prompt to embed based on boolean logic or other conditions using Langfuse prompt variables.
tags: [prompt-management]
---

# Can I dynamically select which sub-prompt to embed based on a condition?

Let's say you have a main prompt with shared instructions, but want to inject a different sub-set of instructions based on whether the user is on a free or premium plan.

[Langfuse prompt references](/docs/prompt-management/features/composability) don't support conditional logic (like `{% if %}` statements) directly. However, you can achieve dynamic prompt selection by handling the logic in your application code and passing the selected prompt as a [variable](/docs/prompt-management/features/variables) value.

**Before diving into the how, let's first zoom out and look at the options you have.**

| Approach                                                                                | Use when                                      | Trade-offs                                                                                                                                                                                                     |
| --------------------------------------------------------------------------------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [**Static prompt references**](/docs/prompt-management/features/composability)          | Sub-prompt is always the same                 | ✅ Fully declarative, resolved automatically in Playground<br/>❌ No dynamic selection based on runtime values                                                                                                 |
| **Dynamic selection via variables** (this page)                                         | Sub-prompt depends on runtime conditions      | ✅ All (sub)prompts managed in Langfuse UI<br/>✅ Selection logic supported (via code)<br/>✅ Variables automatically detected by Langfuse<br/>❌ Not automatically resolved in Playground, prompt experiments |
| [**External templating**](/faq/all/using-external-templating-libraries) (Jinja, Liquid) | Complex conditionals within a single template | ✅ Full flexibility logic-wise<br/>❌ Prompts not automatically resolved in Playground, prompt experiments <br/>❌ Variables not automatically detected by Langfuse                                            |

## How it works

Instead of embedding prompts statically with [prompt composability](/docs/prompt-management/features/composability) (`@@@langfusePrompt:...@@@`), you fetch multiple prompts and select which one to use in your application code:

1. Create a parent prompt with a [variable](/docs/prompt-management/features/variables) placeholder (e.g., `{{selected_prompt}}`)
2. Create the sub-prompts you want to conditionally embed
3. Fetch all prompts using `get_prompt()`
4. Select the appropriate sub-prompt based on your logic
5. Pass the selected prompt text to the parent's `compile()` method

Here's an example in Python:

```python
from langfuse import Langfuse

langfuse = Langfuse()

# Parent prompt in Langfuse:
# "Here is your final prompt:\n\n{{selected_prompt}}"

# Get production versions
parent_prompt = langfuse.get_prompt("parent-prompt")
prompt_a = langfuse.get_prompt("prompt-a")
prompt_b = langfuse.get_prompt("prompt-b")

# Pick based on your boolean or other condition
use_prompt_a = True  # your logic here
selected = prompt_a if use_prompt_a else prompt_b

# Insert into parent
final_prompt = parent_prompt.compile(selected_prompt=selected.prompt)
```

<!-- agent-instructions -->

---

## Agent Instructions

This page is part of the [Langfuse](https://langfuse.com) documentation, published as plain Markdown for AI agents. Every page is available as Markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` header. This page: `https://langfuse.com/faq/all/conditional-prompt-embedding.md`.

### Querying these docs

If the answer is not on this page, query the documentation instead of guessing:

- **Semantic search** across all Langfuse docs, returning an answer with the relevant pages and excerpts. Ask a specific, self-contained question:

  ```bash
  curl -sG "https://langfuse.com/api/search-docs" --data-urlencode "query=How do I trace a LangGraph agent?"
  ```

- **Index of every page**: <https://langfuse.com/llms.txt>, with per-section indexes [llms-docs.txt](https://langfuse.com/llms-docs.txt), [llms-integrations.txt](https://langfuse.com/llms-integrations.txt), and [llms-self-hosting.txt](https://langfuse.com/llms-self-hosting.txt).

### Before writing Langfuse code

- **Install the [Langfuse Agent Skill](https://langfuse.com/docs/api-and-data-platform/features/agent-skill).** It encodes Langfuse's own best practices for instrumentation, prompt management, and evaluation, and materially improves results.
- **Read [What does a good trace look like?](https://langfuse.com/docs/observability/best-practices.md)** before instrumenting an application.
- **Verify endpoints, parameters, and response fields** against the [API reference](https://api.reference.langfuse.com) instead of inferring them from code examples.
- **Use the [Langfuse CLI](https://langfuse.com/docs/api-and-data-platform/features/cli)** (`npx langfuse-cli api <resource> <action>`) to read or write traces, prompts, datasets, and scores from the terminal.

Found an error in these docs? Please open an issue at <https://github.com/langfuse/langfuse-docs/issues>.
