feat(pricing): infer upstream billing provider from model name for proxied endpoints - #53305
Open
vanhoof wants to merge 1 commit into
Open
feat(pricing): infer upstream billing provider from model name for proxied endpoints#53305vanhoof wants to merge 1 commit into
vanhoof wants to merge 1 commit into
Conversation
…oxied endpoints When models are served through a local proxy (vertex-proxy, LiteLLM, custom gateway), the Hermes provider name is a user-defined alias (e.g. "vertex-opus46") that does not match any known billing route. resolve_billing_route() falls through to billing_mode="unknown" and the dashboard shows "unknown" cost for every proxied session. This patch adds _infer_upstream_provider(), which maps well-known model name prefixes (claude-*, gpt-*, gemini-*, deepseek-*, o1-/o3-/o4-*) to their upstream billing provider. The inference runs as a fallback in resolve_billing_route() when no named provider matches, so proxied models resolve to official-docs pricing without any config changes. Also fixes 127.0.0.1 not being recognized as localhost in the custom/local provider detection branch. Signed-off-by: Chris van Hoof <vanhoof@ouwish.com> Assisted-by: hermes-agent/v0.17.0-fd2a35b16 · claude-opus-4.6 · director profile
Contributor
|
Thanks for targeting a verified pricing gap: current Problems
Suggested changes
Automated hermes-sweeper review. |
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.
Problem
When models are served through a local proxy (vertex-proxy, LiteLLM, custom gateway), the Hermes provider name is a user-defined config alias (e.g.
vertex-opus46,litellm) that does not match any known billing route.resolve_billing_route()hits thelocalhostcatch-all and returnsbilling_mode="unknown", so the dashboard and session cost tracker show "unknown" cost for every proxied session.Observed behavior: every API call through a proxy produces
amount_usd=Nonefromestimate_usage_cost(), regardless of the upstream model.Affected setups: any user routing through a local proxy with a custom provider name in
providers:config. This includes vertex-proxy (GCP Vertex AI), LiteLLM, and similar OpenAI-compatible gateways on localhost.Root Cause
resolve_billing_route()checks provider names against a fixed set of known providers (anthropic,openai,openrouter, etc.). User-defined provider names likevertex-opus46fall through every named check and land in thecustom/local/localhostbranch, which returnsbilling_mode="unknown". The official-docs pricing table has the correct entries keyed by(anthropic, claude-opus-4-6), but the billing route never maps the custom provider name toanthropic.Secondary issue: the localhost detection only checked for the string
"localhost"in the base URL, missing127.0.0.1which is the more common form in practice.Fix
Adds
_infer_upstream_provider(), a ~15-line helper that maps well-known model name prefixes to their upstream billing provider:claude-anthropicgpt-openaio1-,o3-,o4-openaigemini-googledeepseek-deepseekThe inference runs as a fallback in two places within
resolve_billing_route():custom/local/localhostbranch (before returningunknown)When a model name matches, the route resolves to
official_docs_snapshotpricing. When it does not match, behavior is unchanged (billing_mode="unknown").Also adds
"127.0.0.1"to the localhost detection alongside"localhost".Per-file summary
agent/usage_pricing.py: adds_MODEL_PREFIX_TO_PROVIDERtable,_infer_upstream_provider()function, and two inference call sites inresolve_billing_route(). Adds"127.0.0.1"to localhost detection.tests/agent/test_usage_pricing.py: 12 new tests covering prefix inference, billing route resolution for proxied models, end-to-end pricing lookup, and end-to-end cost estimation.Design Notes
This is the simplest approach that solves the problem for the vast majority of proxy users without requiring any config changes. A future
billing_providerconfig field on custom providers could override the inference for edge cases, but was deliberately not included to keep the diff minimal and avoid a config schema change.The pattern is consistent with existing inference in
agent_init.py(which infersapi_modefrom URL patterns) and_normalize_anthropic_model_name()/_normalize_bedrock_model_name()(which normalize model name variants for pricing lookup).Related: #18886 attempted to solve this for LiteLLM specifically by probing
/model/info. That approach does not generalize to non-LiteLLM proxies (vertex-proxy, custom gateways) and was closed.Testing
Manually verified against a live vertex-proxy (podman, localhost:8788):
None(unknown)$5.00/$25.00 per M(estimated)None(unknown)$3.00/$15.00 per M(estimated)None(unknown)google(official_docs_snapshot)Signed-off-by: Chris van Hoof vanhoof@ouwish.com