Skip to content

feat(search): report embedding token usage instead of discarding it - #32552

Merged
Vishnuujain merged 2 commits into
mainfrom
feat/embedding-token-usage-hook
Sep 4, 2026
Merged

Vishnuujain merged 2 commits into
mainfrom
feat/embedding-token-usage-hook

Conversation

@Vishnuujain

Copy link
Copy Markdown
Contributor

Closes #32489

Bedrock Titan returns inputTextTokenCount and OpenAI returns usage.prompt_tokens. We read the vector out of the response and throw the rest away, so nothing downstream can tell how many tokens embeddings used.

What this adds

  • An optional UsageListener on EmbeddingClient, called after each successful embedding.
  • A doEmbedWithUsage(text, query) hook that defaults to the current behaviour.
  • Titan and OpenAI now surface the count they already receive.

Notes

  • Backward compatible. A client that only implements doEmbed is unaffected, and GoogleEmbeddingClient / DjlEmbeddingClient need no changes.
  • No schema change, no new dependency, and no cost or billing concepts in the interface.
  • Google and Cohere never send a count, so a listener still has to estimate for those two. It receives the text the provider actually got, after any truncation.
  • The listener runs off the concurrency permit and after the circuit breaker settles. Anything it throws is logged and swallowed, so it can never fail an embedding or open the circuit.

Tests

119 pass. Two of them were checked by breaking the code first: one fails if the doEmbedQuery dispatch is collapsed, one fails if the listener moves back inside the concurrency permit.

@Vishnuujain
Vishnuujain requested a review from a team as a code owner September 3, 2026 09:33
@gitar-bot

gitar-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

Adds optional UsageListener on EmbeddingClient and doEmbedWithUsage hook to surface embedding token counts from Bedrock Titan and OpenAI instead of discarding them. Backward compatible—clients implementing only doEmbed are unaffected, and the listener runs safely outside the concurrency permit with exceptions logged and swallowed. No issues found.

Options

Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@github-actions

github-actions Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@github-actions github-actions Bot added the safe to test Add this label to run secure Github workflows on PRs label Sep 3, 2026
@Vishnuujain Vishnuujain self-assigned this Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

✅ Playwright Results — workflow succeeded

Validated commit 89a50d771b7e8db949f454aeefd38cb65ec2fd79 in Playwright run 33739556374, attempt 1.

✅ 944 passed · ❌ 0 failed · 🟡 3 flaky · ⏭️ 0 skipped · 🧰 0 lifecycle flaky

Performance

Blocking targets: ✅ met · Optimization targets: 🟡 in progress

Shard-job maxima below are not the full workflow wall time; the linked run includes build, fixture, planning, and reporting.

🕒 Full workflow signal wall (to summary) 1h 18m 21s

⏱️ Max setup 5m 3s · max shard execution 17m 7s · max shard-job elapsed before upload 20m 42s · reporting 6s

🌐 206.87 requests/attempt · 2.39 app boots/UI scenario · 36.81% common-shard skew

Optimization targets still in progress:

  • Common shard skew was 36.81% (convergence target: at most 15%).
  • Browser traffic was 206.87 requests per attempt (convergence target: fewer than 200).
  • Application boot ratio was 2.39 per UI scenario (2324 boots / 972 scenarios; convergence target: at most 1).
Shard Passed Failed Flaky Skipped Lifecycle failed Lifecycle flaky
🟡 Shard chromium-01 160 0 1 0 0 0
✅ Shard chromium-02 157 0 0 0 0 0
🟡 Shard chromium-03 137 0 1 0 0 0
🟡 Shard chromium-04 152 0 1 0 0 0
✅ Shard chromium-05 145 0 0 0 0 0
✅ Shard data-asset-rules-01 65 0 0 0 0 0
✅ Shard domain-isolation-01 16 0 0 0 0 0
✅ Shard global-state-01 34 0 0 0 0 0
✅ Shard import-export-01 7 0 0 0 0 0
✅ Shard ingestion-01 2 0 0 0 0 0
✅ Shard reindex-01 28 0 0 0 0 0
✅ Shard search-01 12 0 0 0 0 0
✅ Shard search-rbac-01 29 0 0 0 0 0
🟡 3 flaky test(s) (passed on retry)
  • Pages/Glossary.spec.ts › Approve and reject glossary term from Glossary Listing (shard chromium-01, 1 retry)
  • Pages/Entity.spec.ts › Announcement create, edit & delete (shard chromium-03, 1 retry)
  • Pages/ExplorePageRightPanel_KnowledgeCenter.spec.ts › Should remove user owner for knowledgeCenter (shard chromium-04, 1 retry)

📦 Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

@Vishnuujain
Vishnuujain added this pull request to the merge queue Sep 3, 2026
Merged via the queue into main with commit 2209bd7 Sep 4, 2026
118 of 121 checks passed
@Vishnuujain
Vishnuujain deleted the feat/embedding-token-usage-hook branch September 4, 2026 02:37
harshach pushed a commit that referenced this pull request Sep 8, 2026
…32552)

* feat(search): report embedding token usage instead of discarding it

* fix(search): report the text the provider received, not the caller's input

(cherry picked from commit 2209bd7)
Vishnuujain added a commit that referenced this pull request Sep 8, 2026
…32342) (#32987)

* Fix MCP entity patch lifecycle handling (#32226)

* fix(mcp): respect entity patch lifecycles

* fix(mcp): protect task patch lifecycle

* gitarbot feedback: block document, metric, metricGroup patch lifecycles too

* reflow lifecycle comment so no entry wraps at the 100-col limit

* drop metric and metricGroup entries, reverted upstream by #32333

(cherry picked from commit ac4a26b)

* feat(search): report embedding token usage instead of discarding it (#32552)

* feat(search): report embedding token usage instead of discarding it

* fix(search): report the text the provider received, not the caller's input

(cherry picked from commit 2209bd7)

* Fixes 30023: apply the caller's policies to bot searches (#32460)

* fix(search): apply the caller's policies to bot searches (#30023)

* test: poll for the widened bot's own view in the allow-only role IT

* test: probe search RBAC on the asset index, not the domain index

* test: prove bot search scoping with a deny rule, not domains

* gitarbot feedback: include team ids in the RBAC query cache key

(cherry picked from commit dda6a92)

* fix(search): embed ElasticSearch semantic search queries as queries, not documents (#32553)

(cherry picked from commit ce2a1c1)

* fix(ingestion): use HTTP transport for MCP connector (#32342)

* fix(ingestion): use HTTP transport for MCP connector

* Update generated TypeScript types and dereferenced schemas

* chore: prune obsolete G004 baseline entries

* fix(ingestion): complete MCP HTTP transport support

* style(ingestion): format MCP transport client

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
(cherry picked from commit c45f71a)

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
pmbrull added a commit that referenced this pull request Sep 10, 2026
* feat(data-insights): capture completeness evidence and extend run lifecycle

* feat(search): report embedding token usage instead of discarding it (#32552)

* feat(search): report embedding token usage instead of discarding it

* fix(search): report the text the provider received, not the caller's input

(cherry picked from commit 2209bd7)

* Restrict cross-user follower changes (#32322)

* fix(security): restrict cross-user follower changes

* fix(security): validate follower user IDs

* fix(cache): bypass read-bundle cache for uncached entities

buildReadBundle used the read-bundle cache for every non-deleted read,
ignoring UNCACHED_ENTITY_TYPES (user, task, workflow*, bot, domain, dataProduct).
For those types a mutation followed by an immediate re-read — e.g. the follower
add/delete then fetch in this PR's DataProductResourceIT — could serve a stale
cached bundle. Without this, the follower authorization tests fail under a
cache-enabled deployment (postgres-elasticsearch-redis / postgres-opensearch
integration lanes) with `expected <false> but was <true>` after a delete.

Gate the read-bundle cache on isCacheableEntityType(entityType) so uncached
types take the already-supported bundleCache == null path and always reflect the
current entity_relationship state. Required for the follower tests to pass on the
Redis/OpenSearch lanes (documents why this caching change is part of the security
fix, per reviewer request).

* test(followers): rename put_-prefixed delete-follower tests to delete_ prefix

The two round-trip tests exercise the DELETE follower endpoint (add is setup),
so name them with the delete_ verb prefix to match the method-name convention
(addresses the Copilot review comment).

---------

Co-authored-by: sonika-shah <[email protected]>
(cherry picked from commit 2fb1cb5)

* Fixes #32600: include samples in AI context (#32608)

* feat(api): add samples to AI context

Limit structured table context to 10 permission-filtered rows so SQL agents can inspect value shape without a warehouse discovery query.

* fix(api): resolve sample-data authz with owners loaded

The AI context sample-data check wrapped an already-loaded table in a
pre-resolved ResourceContext. That constructor never runs resolveEntity(),
so owners and domains stayed absent (TABLE_FIELDS does not request them)
and every owner- or domain-conditioned rule misfired in both directions:
an owner-conditioned Deny failed open, and authorizePII() saw null owners
so a table's own owner had their sample data masked.

Resolve by id instead, which loads authorizationFields(), matching what
TableResource's GET /tables/{id}/sampleData already does.

The existing IT used an unconditioned Deny, which passes whether or not
owners are loaded. Add an owner-conditioned Deny case that first asserts
the real endpoint returns 403, then asserts the AI context endpoint
agrees. Verified: it returns 10 sample rows before the fix, none after.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
(cherry picked from commit 8298911)

* fix(search): resolve DI indexes with the cluster data stream prefix

* fix(ui): keep autocomplete responses with their own requests

---------

Co-authored-by: Sriharsha Chintalapani <[email protected]>
Co-authored-by: Vishnu Jain <[email protected]>
Co-authored-by: Sriharsha Chintalapani <[email protected]>
Co-authored-by: sonika-shah <[email protected]>
Co-authored-by: Pere Miquel Brull <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>

This branch was previously deployed

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

Labels

safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embedding clients discard the token counts providers return

2 participants