Langfuse token usage analytics
Quick answer
Use the
langfuse Python SDK to automatically track token usage by wrapping your OpenAI API calls. Initialize Langfuse with your API keys, then decorate or call your LLM functions to collect detailed token usage analytics for monitoring and optimization.PREREQUISITES
Python 3.8+OpenAI API keyLangfuse public and secret keyspip install langfuse openai>=1.0
Setup
Install the langfuse SDK and openai Python package. Set your environment variables for OpenAI and Langfuse API keys to enable token usage tracking.
pip install langfuse openai>=1.0 Step by step
This example shows how to initialize Langfuse and OpenAI clients, then make a chat completion call with automatic token usage analytics captured by Langfuse.
import os
from openai import OpenAI
from langfuse import Langfuse
from langfuse.decorators import observe
# Initialize Langfuse client with public and secret keys
langfuse = Langfuse(
public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
secret_key=os.environ["LANGFUSE_SECRET_KEY"],
host="https://cloud.langfuse.com"
)
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Decorate your function to automatically track token usage
@observe()
def generate_response(prompt: str) -> str:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
if __name__ == "__main__":
prompt = "Explain Langfuse token usage analytics."
answer = generate_response(prompt)
print("Response:", answer) output
Response: Langfuse automatically tracks token usage and other metadata for your OpenAI API calls, enabling detailed analytics and cost monitoring.
Common variations
- Use
@observe()decorator on async functions for asynchronous tracking. - Manually create spans with
langfuse.start_span()for custom instrumentation. - Track other LLM providers by wrapping their clients similarly.
- Configure Langfuse host URL if using a self-hosted instance.
import asyncio
from langfuse.decorators import observe
@observe()
async def async_generate(prompt: str) -> str:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
async def main():
answer = await async_generate("Async Langfuse token usage example.")
print("Async response:", answer)
if __name__ == "__main__":
asyncio.run(main()) output
Async response: Langfuse supports async tracking of token usage with the same ease as sync calls.
Troubleshooting
- If token usage is not appearing in your Langfuse dashboard, verify your
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYenvironment variables are set correctly. - Ensure your OpenAI client calls are wrapped or decorated with
@observe()to enable tracking. - Check network connectivity to
https://cloud.langfuse.comor your configured Langfuse host.
Key Takeaways
- Use the Langfuse Python SDK to automatically capture token usage analytics for OpenAI API calls.
- Decorate your LLM call functions with
@observe()to enable seamless tracking. - Set
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYenvironment variables for authentication. - Langfuse supports both synchronous and asynchronous usage tracking.
- Troubleshoot by verifying keys, network access, and proper function decoration.