feat: surface the LLM Gateway per-request dollar cost instead of dropping it - #136
Merged
Conversation
andreitava-uip
requested review from
DragosBobolea,
cosminacho,
cristipufu,
dragosvelcea,
ionmincu,
ionut-mihalache-uipath,
radu-mocanu and
vldcmp-uipath
as code owners
September 9, 2026 10:28
…ping it Callers can opt in with X-UiPath-LlmGateway-IncludeAssociatedDollarCost: true (Passthrough API). The gateway then adds associated_dollar_cost to non-streaming JSON bodies and appends a trailing frame to streams (SSE data event, AWS event-stream costMetadata message). The vendor SDKs re-parse the body or stop reading at their terminal event, so the value never reached callers. Core: UiPathHttpxClient/UiPathHttpxAsyncClient capture it into a ContextVar for opted-in requests. Non-streaming bodies are read directly; streaming responses are wrapped so the trailing frame is captured on exhaustion, or drained on close when the SDK stops at the vendor's terminal event ([DONE], Bedrock's metadata / invocation-metrics frame). LangChain: invoke()/ainvoke() set response_metadata["associated_dollar_cost"]; stream()/astream() yield one extra empty chunk carrying it after the vendor's last chunk. The Bedrock shim hides the gateway's costMetadata event from langchain-aws. Absence means "not priced", never $0.
Drop tests with no unique failure mode (verified by mutation), fold the counting byte streams into LazyByteStream, use one Bedrock model builder and one llmgw_settings fixture, and move the transport swap into a helper. Add the missing checks for the drain bound and for event-stream dispatch. Remove the dead [DONE] guard in the SSE cost parser.
andreitava-uip
force-pushed
the
feat/keep-llmgw-dollar-cost
branch
from
September 9, 2026 11:39
d75f0e0 to
128681e
Compare
cosminacho
approved these changes
Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Stop dropping the per-request dollar cost the LLM Gateway reports for opted-in calls (
X-UiPath-LlmGateway-IncludeAssociatedDollarCost: true, Passthrough API only). The gateway addsassociated_dollar_costto non-streaming JSON bodies and appends a trailing frame to streams (SSEdata:event, AWS event-streamcostMetadatamessage); the vendor SDKs re-parse the body or stop reading at their terminal event, so callers never saw it.Core (
uipath-llm-client) — newutils/dollar_cost.py. The httpx clients capture the value into a ContextVar for opted-in requests. Non-streaming bodies are read directly. Streaming responses are wrapped: the value is captured when the stream is exhausted, or drained on close when the SDK stops at the vendor's terminal event (openai at[DONE], langchain-aws at Bedrock's stop event). Requested-but-unreadable costs (compressed stream, lost framing, non-numeric value) log a warning.LangChain (
uipath-langchain-client) —invoke()/ainvoke()setresponse_metadata["associated_dollar_cost"];stream()/astream()yield one extra empty chunk carrying it after the vendor's last chunk (the cost is only known then; holding chunks back would delay every token). The Bedrock shim hides the gateway'scostMetadataevent from langchain-aws, which raises on unknown events. Absence means "not priced", never $0.Both packages bump to 1.19.0.
Known residual
Opted-in Gemini streams log langchain-google-genai's "Gemini produced an empty response" warning for the gateway's trailing frame; the cost is still captured.
Tests
Unit tests for the frame parsers and stream wrappers, plus end-to-end tests through the real openai, anthropic and langchain-aws SDKs over lazy per-event mock transports (
UiPathChatOpenAI,UiPathChatBedrockConverse,UiPathChatBedrock,UiPathChatAnthropicBedrock), covering invoke, stream,invoke(streaming=True), opted-out and unpriced cases.