Replies: 1 comment 1 reply
-
|
x-wso2-application-id becomes "default" for unauthenticated calls only? It will happen for both unauthenticated and any API calls with API Keys which are not bound to any applications? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
How It Works
x-wso2-application-id, which is set by theapi-key-authpolicy after successfully authenticating an API key.token-based-ratelimitandllm-cost-based-ratelimit) use key extraction to decide what to count against. Currently the key is justroutename.routename + x-wso2-application-id.Design Decision: Reuse Existing Policies with a Toggle
consumer-token-based-ratelimit,consumer-llm-cost-based-ratelimit), we extend the existingtoken-based-ratelimitandllm-cost-based-ratelimitpolicies with aconsumerBasedboolean parameter.consumerBased(backend, shared counter) and once withconsumerBased: true(consumer, per-app counters). The gateway processes each as an independent instance.Note:
x-wso2-application-idis Not inSharedContext.Metadatametadatakey extraction reads fromSharedContext.Metadata. Currently,api-key-authwrites the application ID to two places after authentication:SharedContext.AuthContext.Properties["ApplicationID"]AnalyticsMetadata["x-wso2-application-id"]SharedContext.Metadata, so the rate limiter cannot find the application ID.SharedContext.Metadatainapi-key-authas part of this change.Changes Required
gateway-controllersapi-key-auth/apikey.go- after a successful auth, writex-wso2-application-idintoSharedContext.Metadataso downstream rate limiting policies can read it.token-based-ratelimit- addconsumerBasedboolean to the policy definition yaml, and intoken_based_ratelimit.goswitch the key extraction to include the application ID metadata when it's true.llm-cost-based-ratelimit-consumerBasedto the policy definition yaml and updatellm_cost_based_ratelimit.goaccordingly.llm_cost_delegate. Fix by using separate keys:llm_cost_delegatefor backend andllm_cost_delegate_consumerfor consumer.advanced-ratelimit/ratelimit.go- two fixes:fallbackfield toKeyComponent. Whenx-wso2-application-idis absent from metadata (e.g. noapi-key-authin the chain), the key used isroutename:default. This prevents any accidental collision with the backend key (routenamevsroutename:defaultare always distinct).advanced-ratelimitpasses state between its request and response phases via hardcoded metadata key names (ratelimit:keys,ratelimit:result, etc.)routenameand the consumer overwrites withroutename:app-id, so the backend's response phase reads the consumer's key and deducts tokens from the wrong counterinstanceIDfield toRateLimitPolicy( a short hash of the quota names and key extraction config) and routing all metadata reads and writes throughp.metaKey(base)which appends the instance IDapi-platformplatform-api/src/internal/service/llm_deployment.go- three things to fix here:addOrAppendPolicyPathcurrently merges all entries with the same policy name, so a backend and a consumertoken-based-ratelimitwould get collapsed into one. Need to make it scope-aware by checking whetherconsumerBaseddiffers between entries.hasPolicy()helper to avoid addingllm-costtwice when both backend and consumer cost limits are configured. Without this we'd end up with twollm-costentries in the chain.gateway/gateway-controller/default-policies/- update bothtoken-based-ratelimit.yamlandllm-cost-based-ratelimit.yamlwhich are copies of the gateway-controllers definitions.apim-saasServiceProviderRateLimitingTab.tsx- the Per Consumer card is currently disabled with a "Coming Soon" chip. Need to remove those.Runtime Flow
Via the Rate Limit tab
Consumer limit only
llm_deployment.gogenerates onetoken-based-ratelimitentry withconsumerBased: true(per-app counter)api-key-authauthenticates it and writes the application ID intoSharedContext.Metadatatoken-based-ratelimitchecks the per-app counter (keyed byroutename + application-id)Both backend and consumer limits configured
llm_deployment.gogenerates twotoken-based-ratelimitentries - one withoutconsumerBased(shared counter) and one withconsumerBased: true(per-app counter)api-key-authauthenticates it and writes the application ID intoSharedContext.Metadatatoken-based-ratelimitchecks the shared counter (keyed byroutename)token-based-ratelimitchecks the per-app counter (keyed byroutename + application-id)Via the Guardrails tab
The Guardrails tab lets users configure policies directly, bypassing
llm_deployment.go. TheconsumerBasedparameter is set explicitly in the policy params rather than being inferred from the rate limit configuration.Consumer cost limit only
llm-cost-based-ratelimitpolicy in the Guardrails tab withconsumerBased: trueand a budget (e.g.$0.1/hour)consumerBased: truein paramsapi-key-authauthenticates it and writes the application ID intoSharedContext.Metadatallm-costreads the response and writes the request cost intoSharedContext.Metadataunderx-llm-costllm-cost-based-ratelimitchecks the per-app cost counter (keyed byroutename + application-id)Both backend and consumer cost limits configured
llm-cost-based-ratelimitentries in the Guardrails tab — one withoutconsumerBased(shared budget) and one withconsumerBased: true(per-app budget)api-key-authauthenticates it and writes the application ID intoSharedContext.Metadatallm-costreads the response cost intoSharedContext.Metadatallm-cost-based-ratelimitchecks the shared cost counter (keyed byroutename)llm-cost-based-ratelimitchecks the per-app cost counter (keyed byroutename + application-id)Requests where app ID is unavailable
llm-cost-based-ratelimitpolicy withconsumerBased: truex-wso2-application-idis missing/empty inSharedContext.Metadata(for example, noapi-key-authin the chain, or any flow where app ID is not populated)"default"is used, so the effective key isroutename:default"default"cost bucket"default"budget is exhausted, further requests without an app ID are blockedBeta Was this translation helpful? Give feedback.
All reactions