fix(ai-connectors): return actual usage tokens in OpenAiCompatibleAdapter and aggregate telemetry across all logs

Open
#51 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript

Research direction

Start with packages/ai-connectors/src/adapters/openai-compatible.adapter.ts and its executeCompletion method, then read AdapterExecuteResult in base.ts to confirm the token fields. Next inspect getTelemetry and dispatchCompletion in apps/api/src/ai-connectors/ai-connectors.service.ts, along with the Prisma aggregation APIs used by this project. Done means provider token counts are preserved and telemetry totals cover all matching logs while recent failovers remain limited to 10.

Written by the indexing model from the issue text.

Description

backend bug good first issue
Problem Description
  1. Actual token usage is discarded for 8 of 9 AI providers:
    In packages/ai-connectors/src/adapters/openai-compatible.adapter.ts (executeCompletion, lines 108-112), the adapter only returns { text, latencyMs, tokensUsed } and omits inputTokens (data?.usage?.prompt_tokens) and outputTokens (data?.usage?.completion_tokens), even though AdapterExecuteResult (base.ts, lines 24-25) defines both fields.
    As a result, AiConnectorsService.dispatchCompletion (apps/api/src/ai-connectors/ai-connectors.service.ts, lines 645-646) falls back to chars / 4 estimates for groq, openai, xai, deepseek, gemini, openrouter, mistral, perplexity, and custom_* connectors.

  2. Telemetry totals are truncated to the latest 100 rows:
    In apps/api/src/ai-connectors/ai-connectors.service.ts (getTelemetry, lines 328-397), findMany({ where: { companyId }, take: 100 }) loads only the most recent 100 AiExecutionLog rows and computes totalInboundTokens, totalOutboundTokens, totalEstimatedCost, totalRequests, and providerStats from that 100-row slice. At 1k-10k+ users, totals cap out and fluctuate as older requests drop out of the window.


Expected Behavior
  1. OpenAiCompatibleAdapter.executeCompletion returns inputTokens and outputTokens from data?.usage (falling back to chars / 4 only when a provider omits usage).
  2. getTelemetry computes workspace totals and per-provider breakdowns using database aggregations across all matching logs (not take: 100), while keeping a small take: 10 query only for recentFailovers.

Where to Look
  • packages/ai-connectors/src/adapters/openai-compatible.adapter.ts (executeCompletion, lines 108-112)
  • apps/api/src/ai-connectors/ai-connectors.service.ts (getTelemetry lines 328-397, dispatchCompletion lines 645-646)

Solution / Implementation Guide
  1. Return actual usage tokens (openai-compatible.adapter.ts):
    • Extract const inputTokens = data?.usage?.prompt_tokens; and const outputTokens = data?.usage?.completion_tokens; and include them in the returned AdapterExecuteResult.
  2. Aggregate telemetry in PostgreSQL (ai-connectors.service.ts):
    • Replace the take: 100 in-memory loop in getTelemetry with prisma.aiExecutionLog.aggregate (for totalInboundTokens, totalOutboundTokens, totalEstimatedCost, totalLatency, totalRequests) and prisma.aiExecutionLog.groupBy({ by: ['providerKey', 'status'], ... }) for providerStats.
    • Fetch recentFailovers with a separate take: 10 query filtered to wasFallback: true or status: 'FAILED'.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.