Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add provider_item_id to ai-gateway tool usage recording - #24562

Closed
dannykopping wants to merge 5 commits into
mainfrom
danny/aigov-96-tool-item-id
Closed

feat: add provider_item_id to ai-gateway tool usage recording#24562
dannykopping wants to merge 5 commits into
mainfrom
danny/aigov-96-tool-item-id

Conversation

@dannykopping

@dannykopping dannykopping commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: implemented by a Coder Agent using Claude Opus 4.6

Summary

Adds a separate provider_item_id column to aibridge_tool_usages to capture the output item's unique ID independently from provider_tool_call_id.

This addresses review feedback from coder/aibridge#273 — conflating the item id and call_id into a single field causes data consistency issues when other sources record these fields correctly.

Why two IDs?

Provider tool call output items can have two distinct identifiers:

Field Semantics Present on
id Unique output item identifier All output items
call_id Correlation ID for matching tool output Agentic tools only (computer_call, shell_call, etc.)

Hosted tools (web_search_call, file_search_call, code_interpreter_call, image_generation_call, mcp_call) only have id — no call_id — because the server executes them internally.

For /v1/chat/completions and Anthropic /v1/messages, only ToolCallID is populated (it's the correlation ID in both APIs). ItemID stays empty since those APIs don't have a separate item ID concept.

Changes

  • Migration 000472: adds nullable provider_item_id column to aibridge_tool_usages
  • Proto: adds item_id field (field 11) to RecordToolUsageRequest
  • Server handler: plumbs item_id through to DB insert + structured logging
  • Translator: maps ToolUsageRecord.ItemID → proto ItemId
  • go.mod: bumps coder/aibridge to 34c9a190 (adds ItemID field)

Dependencies

Depends on coder/aibridge#273 being merged first (the go.mod points at the branch commit).

Linear: AIGOV-96

@dannykopping dannykopping left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review ✅

Comment thread go.mod Outdated
github.com/brianvoe/gofakeit/v7 v7.14.0
github.com/coder/agentapi-sdk-go v0.0.0-20250505131810-560d1d88d225
github.com/coder/aibridge v1.1.3-0.20260420072253-d474d6813420
github.com/coder/aibridge v1.1.3-0.20260421115004-34c9a190da9a

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: replace with merge commit from coder/aibridge#273

@dannykopping
dannykopping requested a review from pawbana April 21, 2026 13:26
@dannykopping
dannykopping marked this pull request as ready for review April 21, 2026 13:26
@dannykopping dannykopping changed the title feat(enterprise/aibridged): add provider_item_id to tool usage recording feat: add provider_item_id to ai-gateway tool usage recording Apr 21, 2026
Adds a separate provider_item_id column to aibridge_tool_usages to
capture the output item's unique ID independently from the
provider_tool_call_id (which is the correlation ID for agentic tools).

This avoids conflating two semantically different identifiers:
- provider_item_id: the output item's unique id (always present)
- provider_tool_call_id: the call_id used to correlate tool output
  (empty for hosted tools like web_search_call)

Changes:
- Migration 000472: adds provider_item_id column
- Proto: adds item_id field (field 11) to RecordToolUsageRequest
- Server handler: plumbs item_id through to DB insert
- Translator: TODO placeholder until aibridge dep is bumped

Depends on: coder/aibridge#273
- Bump github.com/coder/aibridge to 34c9a190 which adds ItemID
  to ToolUsageRecord.
- Wire req.ItemID in the translator (replaces TODO placeholder).
- Remove Responses API-specific wording from migration comment.
The aibridge code was moved into coder/coder (PR #24190), so the
external go.mod dependency is no longer needed. The ItemID field
on ToolUsageRecord is added here since it is required by the
translator and server handler.

Migration renumbered from 000472 to 000475 to avoid conflicts
with migrations added since the original PR was created.
@dannykopping
dannykopping force-pushed the danny/aigov-96-tool-item-id branch from d5f1b68 to 902891b Compare April 23, 2026 08:54

@pawbana pawbana left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change tested?

@dannykopping

Copy link
Copy Markdown
Contributor Author

Is this change tested?

What makes you think it's not?

@pawbana

pawbana commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Is this change tested?

What makes you think it's not?

I don't see any changes to _test.go files 🤔

@dannykopping

Copy link
Copy Markdown
Contributor Author

Is this change tested?

What makes you think it's not?

I don't see any changes to _test.go files 🤔

Oh, that kind of tested. Yeah I can add a test to validate that the field is persisted.

Inserts a tool usage with both ProviderItemID and ProviderToolCallID
set, then reads it back via GetAIBridgeToolUsagesByInterceptionID to
confirm both fields round-trip through the database.
InitiatorID: user.ID,
}, nil)

tool := dbgen.AIBridgeToolUsage(t, db, database.InsertAIBridgeToolUsageParams{

@pawbana pawbana Apr 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think RecordToolUsage function should be tested that it properly stores the ItemID field.
I don't think checking dbgen.AIBridgeToolUsage is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I'm juggling too many topics. Let me get to this when I have a chance to spend some brain tokens.

@github-actions github-actions Bot added the stale This issue is like stale bread. label May 2, 2026
@github-actions github-actions Bot closed this May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants