DocsPrompt ManagementFeaturesLink to Traces

Link Prompts to Traces

Linking prompts to traces enables tracking of metrics and evaluations per prompt version. It’s the foundation of improving prompt quality over time.

After linking prompts and traces, navigating to a generation span in Langfuse will highlight the prompt that was used to generate the response. To access the metrics, navigate to your prompt and click on the Metrics tab.

There are three ways to create traces with the Langfuse Python SDK. For more information, see the SDK documentation.

Decorators

from langfuse import observe, get_client
 
langfuse = get_client()
 
@observe(as_type="generation")
def nested_generation():
    prompt = langfuse.get_prompt("movie-critic")
 
    langfuse.update_current_generation(
        prompt=prompt,
    )
 
@observe()
def main():
  nested_generation()
 
main()

Context Managers

from langfuse import get_client
 
langfuse = get_client()
 
prompt = langfuse.get_prompt("movie-critic")
 
with langfuse.start_as_current_observation(
    as_type="generation",
    name="movie-generation",
    model="gpt-4o",
    prompt=prompt
) as generation:
    # Your LLM call here
    generation.update(output="LLM response")

Manual observations

from langfuse import get_client
 
langfuse = get_client()
 
prompt = langfuse.get_prompt("movie-critic")
 
generation = langfuse.start_generation(
    name="movie-generation",
    model="gpt-4o",
    prompt=prompt
)
 
# Your LLM call here
 
generation.update(output="LLM response")
generation.end()  # Important: manually end the generation

If a fallback prompt is used, no link will be created.

Metrics Reference

Once prompts are linked to traces, Langfuse automatically aggregates the following metrics per prompt version. You can compare them across prompt versions in the Metrics tab in the Langfuse UI:

  • Median generation latency
  • Median generation input tokens
  • Median generation output tokens
  • Median generation costs
  • Generation count
  • Median score value
  • First and last generation timestamp
Was this page helpful?