Skip to main content
Applies to:


Summary Query and aggregate metrics across multiple Braintrust projects using the project_logs() function with multiple project IDs. Since summary shape only supports single projects, use span-level queries with manual aggregation or run separate queries per project.

Configuration Steps

Step 1: Query Multiple Projects Using Span Shape

Pass multiple project IDs to project_logs() and filter for LLM spans to get token metrics.
SELECT
  date_trunc('day', ts) AS day,
  metadata.model,
  sum(metrics.prompt_tokens) AS prompt_tokens,
  sum(metrics.completion_tokens) AS completion_tokens,
  sum(metrics.prompt_cached_tokens) AS cached_tokens,
  sum(metrics.prompt_cache_creation_tokens) AS cache_creation_tokens
FROM project_logs('proj_id_1', 'proj_id_2', 'proj_id_3')
WHERE ts >= now() - interval '30 days'
  AND span_attributes.type = 'llm'
GROUP BY 1, 2
ORDER BY 1;

Step 2: Calculate Costs From Token Metrics

Apply per-model pricing to the aggregated token counts since estimated_cost is only available in summary shape.
# Cost calculation per model
base_prompt_cost = (
    (prompt_tokens - cached_tokens - cache_creation_tokens)
    * input_cost_per_mil_tokens / 1_000_000
)
cached_prompt_cost = (
    cached_tokens * input_cache_read_cost_per_mil_tokens / 1_000_000
)
cache_creation_cost = (
    cache_creation_tokens * input_cache_write_cost_per_mil_tokens / 1_000_000
)
completion_cost = (
    completion_tokens * output_cost_per_mil_tokens / 1_000_000
)

total_cost = (
    base_prompt_cost + cached_prompt_cost +
    cache_creation_cost + completion_cost
)

Step 3: Alternative Approach for Accurate Costs

Query each project separately using summary shape and combine results in your application to preserve pre-calculated cost accuracy.
-- Query each project individually
SELECT
  date_trunc('day', ts) AS day,
  sum(summary.estimated_cost) AS total_cost
FROM project_logs('proj_id_1') WITH (shape = 'summary')
WHERE ts >= now() - interval '30 days'
GROUP BY 1;

-- Repeat for each project, then combine client-side