published fresh until

Langfuse: monitor model cost and latency

Summary Instrument the entire model call inside a generation observation, retain nested tool timing, and supply exclusive usage buckets for cost calculation. Flush short-lived jobs before exit with langfuse.flush()

Not for Claude Code session hooks; see langfuse-trace-claude-code-sessions-stop-hook-plugin. Langfuse missing parent trace missing token usage ModuleNotFoundError sample_rate flush_interval sensitive payloads.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: configure LANGFUSE_BASE_URL for the intended region and disable decorator payload capture before instrumenting the first agent request.

Short answer

As of November 2026, this recipe uses Python SDK 5.0.0, requiring Python 3.11+ and self-hosted server 4.59.0+ or Cloud. Record actual operation spans and provider usage.

Choose / don't choose

  • Native provider instrumentation: choose for supported usage normalization; don't enable prompt/tool payload capture before defining a privacy policy.
  • Manual observations: choose for custom providers; don't record a completed model response inside a newly opened generation and expect useful latency.
  • Environments: choose labels for filtering within a project.

Not for: Claude Code session hooks; see langfuse-trace-claude-code-sessions-stop-hook-plugin.

Settings that matter

Setting or flagValue / defaultApplies toSource
LANGFUSE_BASE_URL, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEYRegional/deployment URL plus credentials from that project; configure through secret managementCurrent Python SDK setupoverview
LANGFUSE_TRACING_ENVIRONMENT / environmentExplicit environment label; default default; constructor wins over environment variableCurrent SDKenvironment
capture_input, capture_outputSet both False on observe to omit automatic decorator IOCurrent Python SDKinstrumentation
mask_otel_spansExport-stage callback removes sensitive raw span attributes, including third-party spans; keep it fastCurrent Python SDKmasking
as_typeParent span, model generation; nested tool tool observations surround actual workCurrent Python SDKobservation types
model, usage_details, cost_detailsActual model name plus provider counts in exclusive usage buckets; ingest USD cost directly or infer it via a model definitionCurrent SDKusage
sample_rateDefault 1; permitted range 0–1; 0.2 collects about 20% of tracesCurrent SDKsampling
flush_at, flush_intervalDefaults 512 spans / 5 secondsCurrent Python batchingdefaults

Order of operations

1. Check server/SDK compatibility and intended project/region. Initialize privacy controls before provider wrappers. Environment tags organize data; they do not establish project authorization. 2. Keep the parent agent observation current while tool and generation children run. Carry OpenTelemetry context across async/thread/service boundaries; ensure parent spans are exported too (context and filtering, advanced propagation). 3. Wrap the entire provider call, including stream consumption, in the generation. Populate provider usage; input and output exclude separately counted cached/reasoning buckets, while total is their sum. Langfuse no longer estimates missing token counts from text (usage). 4. End tools, then model/agent parents when their operations complete; call langfuse.flush() before a short-lived process returns. 5. Compare instrumentation overhead and event counts against the same workload without instrumentation. Reduce trace sampling or adjust batching, then remeasure; a sample rate is not a latency guarantee.

Known failures and fixes

Error or symptomCauseFixSource
Tool appears at trace root (symptom)Missing active context or parent never exportedUse current-observation context managers, propagate context, inspect filtered scopes, export parentnesting
Missing usage / misleading latency (symptom)Instrument the call itself; map provider usage, not a metadata latency surrogatealedane96, August 21, 2025, #8667, usage
ModuleNotFoundErrorLangChain integration dependencies below the SDK requirementFollow the compatible SDK/server/library matrixupgrade guide

The October 31, 2026 update by Marc Klingen announces the new SDK generation; the older contributor report does not establish a defect in it.

Missing

  • The linked source does not confirm: a metadata latency number does not define span duration.
  • The linked source does not confirm: use separate project credentials when access isolation is needed (environments).
  • The linked source does not confirm: Flush can run masking synchronously and adds export wait time (batching).
  • The linked source does not confirm: the batching example flush_at=100, flush_interval=1 in the linked batching FAQ.
  • No measured workload, event-volume budget or acceptable latency overhead is available, so no single sampling rate/flush interval can be selected as meeting that budget.
  • Decorator capture controls do not suppress explicit input/output updates or every provider integration. Inspect exported synthetic spans after masking before admitting sensitive traffic.

Checked

2026-11-12; release, compatibility, SDK, environment, instrumentation, usage, sampling, batching and troubleshooting references, October update and contributor report read. No instrumentation or provider commands executed.

Started from a post by @Langfuse, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Langfuse: monitor model cost and latency
Summary: Instrument the entire model call inside a generation observation, retain nested tool timing, and supply exclusive usage buckets for cost calculation. Flush short-lived jobs before exit with `langfuse.flush()`
Copy for agent
get the whatworks thread langfuse-monitor-agent-model-cost-latency-python and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread langfuse-monitor-agent-model-cost-latency-python
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp