Skip to content

Analytics: Optimize N+1 query issue #1255

Closed

Description

@Ayush8923

Is your feature request related to a problem?
The Analytics API has an N+1 query issue with GET /api/v1/analytics/monthly/chart, resulting in excessive database load and slower response times as the number of LLM calls grows. This problem arises because each row triggers a separate query on model_config.

Describe the solution you'd like

  • Optimize the API to fetch model pricing in a single query instead of multiple queries.
  • Ensure that the solution scales effectively with increasing usage data.
Original issue

Describe the bug
GET /api/v1/analytics/monthly/chart (and /analytics/monthly) runs a separate SELECT ... FROM global.model_config query for every row returned by the main llm_call aggregation, to fetch that model's pricing. One request made ~98 repeated queries. Sentry flagged it as an N+1 query.

The number of queries grows with the number of (month × modality × provider × model) rows, so the API gets slower and puts more load on the database as usage data grows.

To Reproduce

  1. Use an organization/project with LLM calls across several models and months.
  2. Call GET /api/v1/analytics/monthly/chart?metric=cost (any metric triggers it).
  3. Open the request trace in Sentry.
  4. See one aggregation query on llm_call followed by N repeated model_config queries.

Expected behavior
Model pricing should be fetched in a single query, no matter how many rows the aggregation returns.

Activity

  1. changed the title [-]Analytics API: N+1 query on model_config when calculating cost[/-] [+]Analytics: Optimize N+1 query issue[/+] on Oct 14, 2026
  2. self-assigned this
    on Oct 14, 2026
  3. moved this from To Do to In Progress in Kaapi-devon Oct 14, 2026
  4. moved this from In Progress to In Review in Kaapi-devon Oct 14, 2026
  5. moved this from In Review to Closed in Kaapi-devon Oct 14, 2026
  6. github-actions commented on Oct 14, 2026

    @github-actions

    🎉 This issue has been resolved in version 1.8.0-main.5 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions