Context
You want to programmatically retrieve monthly trace costs broken down by workspace for cost allocation and reporting purposes. This allows you to track LangSmith usage costs for different teams or services and include them in automated cost reporting workflows.
Answer
Currently, you can retrieve trace usage data by workspace using the granular usage API, but you'll need to calculate the actual costs yourself since the API doesn't directly provide dollar amounts.
Here's how to get the usage data:
Use the granular usage API endpoint:
curl -X GET \ "https://api.smith.langchain.com/api/v1/orgs/current/billing/usage \ -H "X-Api-Key: <API KEY HERE>" \ -H "Content-Type: application/json" | jq .Combine with the workspaces endpoint to get workspace-specific information and map usage to workspaces.
Calculate costs by applying your pricing model to the trace counts returned by the API.
Adding external IDs to workspaces:
If you need to tag workspaces with external service IDs for easier mapping:
For new workspaces: Configure the
tenant_handlefield to include your external ID when creating the workspaceFor existing workspaces: You can update the
display_nameto include the external ID, though this may not be ideal since it affects the visible workspace name
Note: There is an ongoing feature request to add direct cost breakdown per workspace and improved workspace metadata/tagging capabilities to the API.
Sources: LangSmith API documentation at https://api.smith.langchain.com/redoc