# Prompt Management and Versioning — LLM Observability (Langfuse / LangSmith)

Source: https://www.skillbyai.com/en/llm-observability/lo-prompt-versioning

> Store prompts as versioned objects with labels, fetch them at runtime and link them to traces.

## Prompts as managed configuration

When prompts are hard-coded strings, every wording change needs a code deploy and nobody can tell which version produced a given answer. **Prompt management** treats prompts as versioned configuration. In Langfuse, each save of a prompt creates a new **version**, and **labels** such as `production` or `staging` point to specific versions; your app fetches the prompt by name (the `production` label by default), fills in variables, and moves to a new version when someone reassigns the label, with no code change. LangSmith offers a similar prompt hub with commits and tags. The SDKs cache fetched prompts so you do not add a network call to every request. The observability payoff comes from **linking**: when the generation records which prompt version it used, you can filter traces, costs and scores by version and compare them directly. Keep a fallback prompt in code in case the prompt service is unreachable at startup.

## Prompt release cycle

A prompt is edited, saved as a new version, labelled for an environment, fetched by the app, and its traces are compared with the previous version.

![Four connected stages: edit prompt, new version, label for environment, compare traces by version.](assets/figures/llm-observability/section-8-map.svg) — Figure 8.1: edit -> new version -> label (staging or production) -> compare traces by version

## Create, fetch and link a prompt (Langfuse)

Variables use double curly braces. Passing `prompt=` to the generation links the trace to that prompt version.

```python
from langfuse import get_client

langfuse = get_client()

langfuse.create_prompt(
    name="support-answer",
    type="text",
    prompt="You are a support agent for {{product}}. Answer briefly: {{question}}",
    labels=["staging"],
)

prompt = langfuse.get_prompt("support-answer", label="staging")
text = prompt.compile(product="SkillByAI", question="Is the course free?")

with langfuse.start_as_current_generation(
    name="answer", model="my-chat-model", input=text, prompt=prompt
) as gen:
    gen.update(output="Yes, all courses are free.")

langfuse.flush()
```

## Who can change production?

Moving the `production` label changes live behaviour without a deploy. Treat it like a deploy: restrict who can do it, run the dataset experiment first, and note the change so a quality drop can be traced back to it.

**Quiz:** In Langfuse prompt management, what does moving the `production` label to version 7 do?

- [ ] Deletes versions 1 to 6
- [ ] Requires redeploying the application
- [x] Makes apps that fetch the production label start using version 7, without a code change
- [ ] Renames the prompt

*Answer:* Makes apps that fetch the production label start using version 7, without a code change. Labels are pointers to versions. Apps fetching by label pick up the new version (after their cache refreshes) while old versions remain available for rollback.
