Repository navigation
Description
Friction
- The ai-spend-telemetry dashboard and
devops ai costleave out every embedding request devops-cli sends, so most gateway traffic is invisible. - In the 24 h ending 2026-10-17 13:20 UTC the gateway served 136
bge-m3:latest/v1/embeddingsrequests with about 4.29M input tokens. That is 72% of its 190 requests and about 89% of its input tokens. - None of them appears in
devops_cli_ai_requests_total,devops_cli_ai_tokens_total,devops_cli_ai_local_cost_equivalent_usd_totalor either spend ledger. - Every embedding caller is affected: RAG index and query, the per-file RAG step in reviews, library ingest and query, roadmap intake's duplicate check, the builtin search tool, the MCP RAG and library tools, the embedding benchmark and the dimension probe.
- Recording them as the code stands would overstate savings.
_emit_local_cost_equivalentprices every local token at the chat reference model, so 4.29M tokens a day at gpt-4o input ($2.50/M) would add about $10.73 a day, against about $0.09 at text-embedding-3-small ($0.02/M).
Evidence
Checked at main 3dd7fa6 (v0.2.36) on 2026-10-17, read-only, no model request sent.
track_request_spend(src/devops_cli/ai/spend/ledger.py:620-719) is the only writer of the ledger and the only emitter of the three counters (:706-718,:601-617).- Its callers are
src/devops_cli/ai/client/unified.py:450,:586,:912andsrc/devops_cli/ai/direct.py:118,:164. All are chat paths. EmbeddingsEngine(src/devops_cli/ai/rag/embeddings.py) is the only client for gateway/v1/embeddingsand Ollama/api/embed, and it never callstrack_request_spend.- Gateway path:
embed_texts:717→_dispatch_embed:689→_embed_openai:983→_embed_single_openai_batch:950and_embed_parallel_openai_batches:956→_post_openai_batch_with_retry:902. - Ollama path:
_query_ollama_node_batch:780. The dimension probe_probe_openai_dimension:640sends one more request. _openai_reply_vectors(:185-188) keepsdata[].embeddingand drops the reply'susage.prompt_tokens.- The pydantic-ai Embedder wrappers estimate tokens as words x 1.3 (
src/devops_cli/ai/agents/embeddings.py:91,src/devops_cli/ai/rag/embeddings.py:1085) and record nothing. - The file records only text counts (
devops_cli_embedding_cache_*,:737-739,:560) and batch-size metrics (:447-474). - Live, 24 h: gateway
litellm_proxy_total_requests_metric_total{route="/v1/embeddings",requested_model="bge-m3:latest"}rose 136.0, all status 200, all with user agentpython-httpx2/2.13.1(the httpx2 pin inuv.lock). - Live, 7 d: no
devops_cli_ai_*series carries an embedding model. - Both ledgers (repo
.data/ai/spend.dband the user-level one), opened read-only, hold onlychat,chat_dispatchandchat_cache_hitrows. There has never been an embedding row. - The requests are devops-cli's:
devops_cli_embedding_cache_misses_total{job="devops-cli"}is non-zero in the same hours, and Open WebUI embeds against Ollama directly (k8s/llm/values-open-webui.yaml:38-41). - genai-prices 0.1.9 (offline) prices
text-embedding-3-smallat $0.02/M under provideropenai. The chat reference default isgpt-4o-mini(src/devops_cli/config/defaults.py:65). - The worker threads already copy the caller's context (
ContextPropagatingThreadPoolExecutor,src/devops_cli/ai/rag/embeddings.py:64,src/devops_cli/telemetry/tracer.py:58-63), so the spend stage and call observers reach them. - Call observers feed the review profile:
ReviewProfiler.observe(src/devops_cli/ai/review/profile.py:240-259) adds every non-cached call tollm_callsand the token totals.
Remedy
Tests first, in tests/test_rag_embeddings.py and tests/test_ai_spend_ledger.py, with an httpx2 MockTransport behind the engine's client and a tmp_path ledger. No network, cluster or model. Each case fails on today's code:
- One gateway batch writes one ledger row:
request_type="embedding",prompt_tokensfrom the reply'susage.prompt_tokens,completion_tokens=0,served_byfrom thex-litellm-model-api-baseheader. - That batch emits one request point and one prompt-token point with
model="bge-m3:latest"andoperation="embeddings". - A batch retried on 429 and then answered writes one row. Parallel batches write one row each.
- An Ollama
/api/embedbatch takes its tokens fromprompt_eval_count. - An
embed_textscall answered wholly from the L1 or Valkey cache writes at most onecached=Truerow with 0 tokens, not one per text. - The dimension probe writes one row.
- Local savings for an embedding row use the embedding reference price, not the chat reference.
- A review profile does not count embedding rows in
llm_callsor its chat token totals; it reports them apart.
Code:
- Record once per HTTP request, where it is sent: in
_embed_single_openai_batchafter_post_openai_batch_with_retryreturns, in_query_ollama_node_batch, and in_probe_openai_dimension. - Pass the same
providerandservervalues the chat path uses, sois_localdecides the same way. - Use the reply's token count. Drop the words x 1.3 estimate in both Embedder wrappers in favour of the engine's count.
- Add an
operationlabel to the request and token counters with the OTel GenAIgen_ai.operation.namevalues (chat,embeddings) fromGenAiOperationNameValuesinopentelemetry-semantic-conventions, declared inpyproject.tomlfor deptry. - Group the dashboard's request-rate and token-rate panels by
operation(k8s/monitoring/dashboards/ai-spend.json:765,:820). - Add
ai.reference_embedding_model(defaulttext-embedding-3-small, insrc/devops_cli/config/defaults.py), priced through the same genai-prices registry. _emit_local_cost_equivalentand the ledger's report-time savings (ledger.py:341-351,:385-402) use it whenrequest_type == "embedding".ReviewProfiler.observekeeps embedding calls apart fromllm_calls, so review benchmarks do not shift.- Order: land before refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001, refactor(ai): Valkey, Qdrant and the embedder each have one seam, with shared fakes and contract suites #1317 and fix(security): move the remaining HTTP clients onto the egress-checked factory and delete the pre-flight resolve paths #1381, which also rework
embeddings.py. They keep the recording at their new seam. - Pending a person: over a day of normal use,
sum(increase(devops_cli_ai_requests_total{operation="embeddings"}[24h]))is within a few percent of the gateway's/v1/embeddingscount. Hourly agreement waits for fix(telemetry): devops-cli metrics reach prometheus in one hop, a late sample no longer drops a whole batch, and the collector runs only what its pipelines use #1132.
Proposed: type/bug, P1-High. It is the largest share of the reported gap, and the savings-reference item depends on its operation label and embedding reference.
Proposed design
Problem statement
Embedding requests made by devops-cli are not being recorded in the AI spend ledger or usage counters, leading to invisible costs and inaccurate reporting. This affects all embedding callers including RAG index/query, library ingest/query, roadmap intake's duplicate check, builtin search tool, MCP tools, and embedding benchmark/probe.
Acceptance criteria
- Criterion 1: Embedding requests are recorded in the spend ledger with correct token counts
Verification: After implementation, gateway/v1/embeddingsrequests should create rows in the ledger withrequest_type="embedding",prompt_tokensfrom reply's usage, andcompletion_tokens=0 - Criterion 2: Embedding requests emit OpenTelemetry metrics with operation label
Verification: The countersdevops_cli_ai_requests_total,devops_cli_ai_tokens_totalshould include points withoperation="embeddings"for embedding calls - Criterion 3: Local cost calculations use embedding reference model instead of chat reference
Verification: When calculating local cost equivalent, the system should useai.reference_embedding_model(default text-embedding-3-small) pricing instead of gpt-4o-mini pricing - Criterion 4: Review profiles do not count embedding rows in chat token totals
Verification: ReviewProfiler.observe should keep embedding calls separate from llm_calls and not add them to chat token counts
Key questions
- Which functions need to be modified to record embedding requests?: Functions that handle embedding requests including
_embed_single_openai_batchafter_post_openai_batch_with_retryreturns,_query_ollama_node_batch, and_probe_openai_dimensioninsrc/devops_cli/ai/rag/embeddings.py[sources: src/devops_cli/ai/rag/embeddings.py] - How should the embedding requests be tracked in the ledger?: Each embedding request should call
track_request_spendwithrequest_type="embedding", using tokens from the API response instead of estimates [sources: src/devops_cli/ai/spend/ledger.py] - What is the embedding reference model to be used for local cost calculations?: The default embedding reference model is
text-embedding-3-smallwhich should be configurable viaai.reference_embedding_model[sources: src/devops_cli/config/defaults.py] - How should the operation label be added to the OpenTelemetry metrics?: Add an
operationlabel with valueembeddingsto the request and token counters in the telemetry emissions [sources: src/devops_cli/ai/spend/ledger.py] - Should embedding calls be counted in review profiles?: No, embedding calls should be kept separate from
llm_callsand chat token totals in ReviewProfiler.observe to avoid skewing benchmarks [sources: src/devops_cli/ai/review/profile.py]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
dan-petty commentedon Oct 18, 2026
Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.
The candidate fixes a bug where embedding requests are not recorded in the spend ledger and AI usage counters.The bug causes significant inaccuracies in cost tracking and dashboard visibility for embedding requests.Embedding requests represent a large portion of gateway traffic and are critical for accurate cost monitoring.Requires changes to the embedding engine and ledger recording logic, but tests can be isolated.dan-petty commentedon Oct 18, 2026
Status set to Ready at 25e2cda (see proposed design in issue body).
dan-petty commentedon Oct 18, 2026
Amendment (2026-10-18): folded here by intake under the agent filing quota
The cycle's agent filing allowance (#1153) was used, so
devops roadmap intakefiled nothing and named this item as the nearest. The candidate below is related work that can land with this item or be filed on its own in a later cycle. Candidate title: "fix(ai): every devops-cli install writes and reads one spend ledger, so devops ai cost counts every recorded call".Friction
devopsbinary ran, not only on the working directory.<repo>/.data/ai/spend.db. Any installed copy writes~/.local/share/devops-cli/.data/ai/spend.db, wherever it runs.devops ai cost,cost report,roi,prometheusandresetread one ledger, picked by the same rule. Run in the repo, they missed 6 of the 54 gateway chat calls in the 24 h ending 2026-10-17 13:20 UTC (11%), with their tokens and savings./usr/local/bin/devops. A shell where it comes before.venv/binruns older code than the checkout and writes the user-level ledger.devops roadmap intakeruns that resolved to that copy.Evidence
Checked at main 3dd7fa6 (v0.2.36) on 2026-10-17, read-only. Ledgers opened read-only.
SpendLedger's default path isresolve_review_data_path(settings.data.dir)/ai/spend.db(src/devops_cli/ai/spend/ledger.py:226-237).review_data_root()returns the main worktree only whenis_own_source_repository()(src/devops_cli/core/repo.py:318-323,:274-291). An installed copy is never the source (_is_installed_copy,_source_repository,:209-238).own_source_repo Falseand the user data root;.venv/bin/pythongivesTrueand the repo..devcontainer/Dockerfile:38-43runsuv pip install --system --no-deps /tmp/devops-cli.src/devops_cli/commands/ai_cost.py:327,:402,:469,:563and:740.ledger.py:33-51), so today the file location is the only per-repo split.Remedy
Tests first, in
tests/test_ai_spend_ledger.pyandtests/test_core_repo.py, withtmp_pathroots and a fake installed-copy location. No network:devops ai cost reportover that ledger counts rows written by both.Code (owner decision on #972's placement):
user_data_root()/ai/spend.db, or under an absolutedata.dirwhen one is set, independent of cwd and install type.sqlite3..devcontainer/Dockerfile:38-43, or keep it for bootstrap only and make.venv/bincome first onPATHin every shell (login, non-login and tool shells). Owner choice.changelog.d/<n>.mdunder### Fixed.Proposed: type/bug, P2-Medium.
fix(monitoring): the ai spend dashboard shows local savings for every…