by thetestingacademy
Instrument LLM apps with Langfuse tracing, then use traces, scores, and datasets to test in production, run evaluations on real traffic, catch regressions, and close the loop from incident to golden dataset.
npx @qaskills/cli add langfuse-llm-observabilityAuto-detects your AI agent and installs the skill. Works with Claude Code, Cursor, Copilot, and more.
You are an expert AI quality engineer specializing in Langfuse. When the user asks you to instrument, monitor, or test an LLM application using traces and production data, follow these instructions.
pip install langfuse # python
npm install langfuse # typescript
export LANGFUSE_PUBLIC_KEY=pk-...
export LANGFUSE_SECRET_KEY=sk-...
export LANGFUSE_HOST=https://cloud.langfuse.com # or self-hosted URL
from langfuse import Langfuse, observe
langfuse = Langfuse()
@observe() # creates a trace per call
def answer(user_id: str, session_id: str, query: str):
langfuse.update_current_trace(user_id=user_id, session_id=session_id,
tags=["support-bot", "prod"])
chunks = retrieve(query) # decorate with @observe() too: becomes a span
reply = generate(query, chunks) # generations auto-capture model, tokens, cost
return reply
Decorate retrieval, reranking, generation, and tool calls separately; a flat trace cannot localize failures. Wrappers/integrations exist for OpenAI, LangChain, LlamaIndex, and the Vercel AI SDK; prefer them over manual spans.
# 1. User feedback from the app (thumbs up/down)
langfuse.create_score(trace_id=trace_id, name="user-feedback", value=0, comment="wrong policy quoted")
# 2. Automated LLM-as-judge on sampled traces (configure evaluators in the UI
# or run your own job):
from my_judges import faithfulness_judge
for trace in fetch_traces(tags=["support-bot"], sample=0.1):
score = faithfulness_judge(trace.input, trace.output, trace.metadata["contexts"])
langfuse.create_score(trace_id=trace.id, name="faithfulness", value=score)
# 3. Human annotation queues in the UI for calibration batches
Alerting policy: dashboard the 7-day moving average per score name; investigate any sustained drop even inside "acceptable" range, since judge drift and product drift look identical until triaged.
The core testing workflow: bad trace -> dataset item -> offline eval -> CI gate.
# 1. Curate: add a failing production trace to a dataset
langfuse.create_dataset(name="support-golden")
langfuse.create_dataset_item(
dataset_name="support-golden",
input={"query": "Can I get a refund after 45 days?"},
expected_output="No; refund window is 30 days. Offer credit options.",
source_trace_id=bad_trace.id, # provenance
)
# 2. Experiment: run a candidate change against the dataset
dataset = langfuse.get_dataset("support-golden")
for item in dataset.items:
with item.run(run_name="prompt-v9") as root:
output = my_app.answer_candidate(item.input["query"])
root.update(output=output)
root.score(name="correctness", value=judge(output, item.expected_output))
# 3. Compare runs in the UI (prompt-v8 vs prompt-v9) or via API in CI
CI gate pattern: nightly job runs the current build against the dataset, pushes scores as a run, and fails if aggregate correctness drops below threshold or below the previous run by more than 2 points.
Store prompts in Langfuse prompt management with labels (production, staging). Testing rules: every prompt version change runs the dataset experiment BEFORE the production label moves; traces record which prompt version served each request, so incidents bisect to prompt versions in seconds.
| View | Bug class it catches |
|---|---|
| Traces filtered by low user-feedback | Real failures, source for dataset items |
| Score trend by prompt/model version | Regressions from "harmless" prompt edits |
| Cost per trace over time | Token explosions from context stuffing |
| Latency percentiles per span | Slow retrieval hiding behind fast generation |
| Sessions with high turn count | Loops, users re-asking because answers fail |
- name: Install QA Skills
run: npx @qaskills/cli add langfuse-llm-observability12 of 29 agents supported
Build AI agents that write, run, and fix tests. Playwright, LLM evals, and CI in one live cohort.
Use code AITESTER at checkout