Expose exact LLM token usage for agentic queries - #2580
Conversation
…vider usage through Python, CLI, service, and MCP paths so callers can report inner ReAct tokens without approximation.
Greptile SummaryThis PR collects provider-reported token usage from the ReAct and final-selection stages and exposes normalized totals through optional Python, CLI, REST, and MCP response metadata.
|
| Filename | Overview |
|---|---|
| nemo_retriever/src/nemo_retriever/_agentic/nemo_agent/llm/usage.py | Adds normalization across OpenAI-style and input/output token aliases while preserving exact stage breakdowns. |
| nemo_retriever/src/nemo_retriever/query/agentic.py | Adds per-query usage collection and positional-to-caller query ID mapping; the new public interfaces need complete contract documentation. |
| nemo_retriever/src/nemo_retriever/query/workflow.py | Adds the typed hits-plus-usage workflow result and cleanup path, but leaves the new public contract under-documented. |
| nemo_retriever/src/nemo_retriever/service/query_schema.py | Adds an additive agentic response subtype with optional normalized usage. |
| nemo_retriever/src/nemo_retriever/service/vectordb_app.py | Routes agentic requests through the new response model without changing classic response serialization. |
| nemo_retriever/src/nemo_retriever/cli/query/app.py | Adds the agentic-only include-usage flag while retaining the legacy hits-list output by default. |
Sequence Diagram
sequenceDiagram
participant Caller
participant Workflow
participant ReAct
participant Selection
Caller->>Workflow: Agentic query
Workflow->>ReAct: Run query with positional ID
ReAct-->>Workflow: Ranked candidates + stage usage
Workflow->>Selection: Select final documents
Selection-->>Workflow: Final ranking + stage usage
Workflow->>Workflow: Pop, merge, and normalize usage
Workflow-->>Caller: Hits + optional usage
Prompt To Fix All With AI
### Issue 1
nemo_retriever/src/nemo_retriever/query/workflow.py:30-33
**Incomplete public usage contracts**
The new public result classes and usage-returning methods omit field, parameter, return, and error documentation, leaving API consumers without a defined usage shape or ownership contract. The same incomplete pattern also appears on `AgenticRetrieveResult`, `retrieve_with_usage()`, and both operator `pop_query_usage()` methods.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Reviews (1): Last reviewed commit: "Expose exact token usage for agentic que..." | Re-trigger Greptile
| @dataclass(frozen=True) | ||
| class AgenticQueryDocumentsResult: | ||
| hits: list[dict[str, Any]] | ||
| usage: dict[str, Any] |
There was a problem hiding this comment.
Incomplete public usage contracts
The new public result classes and usage-returning methods omit field, parameter, return, and error documentation, leaving API consumers without a defined usage shape or ownership contract. The same incomplete pattern also appears on AgenticRetrieveResult, retrieve_with_usage(), and both operator pop_query_usage() methods.
Rule Used: Every public class and function in nemo_retriever ... (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: nemo_retriever/src/nemo_retriever/query/workflow.py
Line: 30-33
Comment:
**Incomplete public usage contracts**
The new public result classes and usage-returning methods omit field, parameter, return, and error documentation, leaving API consumers without a defined usage shape or ownership contract. The same incomplete pattern also appears on `AgenticRetrieveResult`, `retrieve_with_usage()`, and both operator `pop_query_usage()` methods.
**Rule Used:** Every public class and function in nemo_retriever ... ([source](https://github.com/nvidia/nemo-retriever/blob/a48f2b12cccbcdec8ad6465b03821d6f4cc9fc80/nemo_retriever/.greptile/config.json))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Description
retriever query --agentic --include-usageWhy
Consumers need exact inner-agent token accounting. Parsing logs or estimating from text does not provide reliable provider-reported usage.
Implementation
pop_query_usage()callsfinallyblocks so backend state is cleared safelyusage: nullwhen the provider does not report usageChecklist