| name | langfuse-cost-tuning |
| description | Monitor and optimize LLM costs using Langfuse analytics and dashboards.
Use when tracking LLM spending, identifying cost anomalies,
or implementing cost controls for AI applications.
Trigger with phrases like "langfuse costs", "LLM spending",
"track AI costs", "langfuse token usage", "optimize LLM budget".
|
| allowed-tools | Read, Write, Edit |
| version | 1.12.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","langfuse","monitoring","llm","analytics"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Langfuse Cost Tuning
Overview
Track, analyze, and optimize LLM costs using Langfuse's built-in token/cost tracking, the Metrics API for programmatic cost analysis, model routing for cost reduction, and automated budget alerts.
Prerequisites
- Langfuse tracing with token usage captured (via
observeOpenAI or manual usage fields)
- For Metrics API:
@langfuse/client installed
- Understanding of LLM pricing models
How Langfuse Tracks Costs
Langfuse automatically calculates costs for supported models (OpenAI, Anthropic, Google) when token usage is captured. For custom models, you can configure pricing in the Langfuse UI under Settings > Model Definitions.
Cost tracking works on observations of type generation and embedding. The observeOpenAI wrapper captures usage automatically; for manual tracing, include usage in your observation updates.
Instructions
Step 1: Ensure Token Usage is Captured
import { observeOpenAI } from "@langfuse/openai";
const openai = observeOpenAI(new OpenAI());
import { startActiveObservation, updateActiveObservation } from "@langfuse/tracing";
await startActiveObservation(
{ name: "llm-call", asType: "generation" },
async () => {
updateActiveObservation({ model: "gpt-4o" });
const response = openai...({
: ,
: [{ : , : prompt }],
});
({
: response.[]..,
: {
: response.?.,
: response.?.,
: response.?.,
},
});
}
);