LangSmith token usage tracking
Quick answer
Use the
langsmith Python SDK's Client to access token usage data by querying runs or traces. You can retrieve token counts from run metadata or use LangSmith's API to monitor usage programmatically.PREREQUISITES
Python 3.8+pip install langsmithLangSmith API key set in environment variable LANGSMITH_API_KEY
Setup
Install the langsmith Python package and set your API key in the environment variable LANGSMITH_API_KEY to authenticate requests.
pip install langsmith Step by step
This example shows how to initialize the LangSmith client, fetch recent runs, and print token usage details for each run.
import os
from langsmith import Client
# Initialize LangSmith client with API key from environment
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])
# Fetch recent runs (limit 5)
runs = client.runs.list(limit=5)
for run in runs:
print(f"Run ID: {run.id}")
# Token usage is available in run.metrics or run.extra
token_usage = run.metrics.get('tokens') or run.extra.get('tokens')
if token_usage:
print(f"Token usage: {token_usage}")
else:
print("Token usage data not available.")
print("---") output
Run ID: 1234abcd Token usage: 150 --- Run ID: 5678efgh Token usage: 230 --- Run ID: 9abcijkl Token usage: 180 ---
Common variations
You can track token usage asynchronously or integrate LangSmith tracing decorators to automatically capture usage during LLM calls.
For example, use @traceable decorator to wrap functions and then query token usage from the recorded runs.
import asyncio
from langsmith import Client, traceable
import os
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])
@traceable
async def generate_text(prompt: str) -> str:
# Simulate LLM call
return f"Response to: {prompt}"
async def main():
response = await generate_text("Hello LangSmith")
print(response)
# After function runs, fetch latest run for token usage
runs = client.runs.list(limit=1)
for run in runs:
print(f"Tracked tokens: {run.metrics.get('tokens')}")
asyncio.run(main()) output
Response to: Hello LangSmith Tracked tokens: 42
Troubleshooting
- If token usage is missing, ensure your LangSmith client is properly authenticated with a valid API key.
- Check that your runs or traces actually include token metrics; some runs may not record usage if tracing is disabled.
- Use the latest
langsmithSDK version to avoid compatibility issues.
Key Takeaways
- Use the LangSmith Python SDK
Clientto programmatically fetch token usage from runs. - Token usage data is stored in run metrics or extra metadata fields.
- Enable tracing decorators to automatically capture token usage during LLM calls.