fix(ai-connectors): return actual usage tokens in OpenAiCompatibleAdapter and aggregate telemetry across all logs
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
- Domain
- backend, databases, observability
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
Problem Description
-
Actual token usage is discarded for 8 of 9 AI providers:
Inpackages/ai-connectors/src/adapters/openai-compatible.adapter.ts(executeCompletion, lines 108-112), the adapter only returns{ text, latencyMs, tokensUsed }and omitsinputTokens(data?.usage?.prompt_tokens) andoutputTokens(data?.usage?.completion_tokens), even thoughAdapterExecuteResult(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 tochars / 4estimates forgroq,openai,xai,deepseek,gemini,openrouter,mistral,perplexity, andcustom_*connectors. -
Telemetry totals are truncated to the latest 100 rows:
Inapps/api/src/ai-connectors/ai-connectors.service.ts(getTelemetry, lines 328-397),findMany({ where: { companyId }, take: 100 })loads only the most recent 100AiExecutionLogrows and computestotalInboundTokens,totalOutboundTokens,totalEstimatedCost,totalRequests, andproviderStatsfrom that 100-row slice. At 1k-10k+ users, totals cap out and fluctuate as older requests drop out of the window.
Expected Behavior
OpenAiCompatibleAdapter.executeCompletionreturnsinputTokensandoutputTokensfromdata?.usage(falling back tochars / 4only when a provider omitsusage).getTelemetrycomputes workspace totals and per-provider breakdowns using database aggregations across all matching logs (nottake: 100), while keeping a smalltake: 10query only forrecentFailovers.
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(getTelemetrylines 328-397,dispatchCompletionlines 645-646)
Solution / Implementation Guide
- Return actual usage tokens (
openai-compatible.adapter.ts):- Extract
const inputTokens = data?.usage?.prompt_tokens;andconst outputTokens = data?.usage?.completion_tokens;and include them in the returnedAdapterExecuteResult.
- Extract
- Aggregate telemetry in PostgreSQL (
ai-connectors.service.ts):- Replace the
take: 100in-memory loop ingetTelemetrywithprisma.aiExecutionLog.aggregate(fortotalInboundTokens,totalOutboundTokens,totalEstimatedCost,totalLatency,totalRequests) andprisma.aiExecutionLog.groupBy({ by: ['providerKey', 'status'], ... })forproviderStats. - Fetch
recentFailoverswith a separatetake: 10query filtered towasFallback: trueorstatus: 'FAILED'.
- Replace the
- Dominant language
- TypeScript
- Stars
- 2
- Forks
- 1
- Avg merge
- 6m
- Merged PRs (30d)
- 27
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sumamakhan761/tryinstance
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#65 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
sumamakhan761/tryinstance#64 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
sumamakhan761/tryinstance#63 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#62 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#60 ·
Maintainers usually reply within 1 day
All issues in sumamakhan761/tryinstance
Similar issues
-
prio:low scope:hud
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
OliverRomhagen/vrigstad-joyride#953 ·
Maintainers usually reply within 1 day
-
review-followup
Difficulty 1/5 Under an hour Newbie friendliness 92/100
griffinwork40/agent-afk#5446 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
kacper-szewczyk/Ticknest#346 ·
Maintainers usually reply within 1 day
-
echarts geo: a GeoJSON URL that returns 200 with a body that is not JSON is fetched on each refreshOpenbug plugin/echarts/geo
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Yeachan-Heo/gajae-code#8230 ·
Maintainers usually reply within 1 day