feat: support runtime contextWindowSize and pricingPerMillion via AgentOptions#8
Open
YangLuYang wants to merge 1 commit intocodeany-ai:mainfrom
Open
feat: support runtime contextWindowSize and pricingPerMillion via AgentOptions#8YangLuYang wants to merge 1 commit intocodeany-ai:mainfrom
YangLuYang wants to merge 1 commit intocodeany-ai:mainfrom
Conversation
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.
Background
Currently,
getContextWindowSize,MODEL_PRICING, andestimateCostintokens.tsuse hardcoded model name matching, which has two core issues:Cannot accurately obtain model context window size, causing context compression to fail: When using models not covered by hardcoding (e.g., third-party models, newly released models, or specific deployment versions),
getContextWindowSizereturns the default value of 200,000, which may differ greatly from the actual model context window.Cannot correctly evaluate token consumption value: The same issue exists in
estimateCost- unknown models use the default pricing (3/1M input, 15/1M output), which does not match actual costs.Solution
This PR adds
contextWindowSizeandpricingPerMillionas optional fields toAgentOptionsandQueryEngineConfig. These values take priority if provided; otherwise, it falls back to the original hardcoded logic.Changes
Types
AgentOptions: AddedcontextWindowSize?: numberandpricingPerMillion?: { input: number; output: number }QueryEngineConfig: Added the same two fieldsFunctions Extended with Optional Override Parameters
getContextWindowSize(model, contextWindowSize?): ReturnscontextWindowSizeif provided, otherwise falls back to hardcoded model matchinggetAutoCompactThreshold(model, contextWindowSize?): Passes through togetContextWindowSizeestimateCost(model, usage, pricing?): Usespricingif provided, otherwise falls back to hardcoded pricing lookupshouldAutoCompact(messages, model, state, contextWindowSize?): PassescontextWindowSizethrough togetAutoCompactThresholdCall Sites Updated
engine.ts:shouldAutoCompactandestimateCostcalls now pass config valuesagent.ts:QueryEngineconstruction passescontextWindowSizeandpricingPerMillionfromAgentOptionsUsage Examples
Backward Compatibility
All new parameters are optional. When not provided, behavior is identical to before this PR.