Skip to content

feat: support runtime contextWindowSize and pricingPerMillion via AgentOptions#8

Open
YangLuYang wants to merge 1 commit intocodeany-ai:mainfrom
YangLuYang:feature/model-config-override
Open

feat: support runtime contextWindowSize and pricingPerMillion via AgentOptions#8
YangLuYang wants to merge 1 commit intocodeany-ai:mainfrom
YangLuYang:feature/model-config-override

Conversation

@YangLuYang
Copy link
Copy Markdown

Background

Currently, getContextWindowSize, MODEL_PRICING, and estimateCost in tokens.ts use hardcoded model name matching, which has two core issues:

  1. 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), getContextWindowSize returns the default value of 200,000, which may differ greatly from the actual model context window.

  2. 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 contextWindowSize and pricingPerMillion as optional fields to AgentOptions and QueryEngineConfig. These values take priority if provided; otherwise, it falls back to the original hardcoded logic.

Changes

Types

  • AgentOptions: Added contextWindowSize?: number and pricingPerMillion?: { input: number; output: number }
  • QueryEngineConfig: Added the same two fields

Functions Extended with Optional Override Parameters

  • getContextWindowSize(model, contextWindowSize?): Returns contextWindowSize if provided, otherwise falls back to hardcoded model matching
  • getAutoCompactThreshold(model, contextWindowSize?): Passes through to getContextWindowSize
  • estimateCost(model, usage, pricing?): Uses pricing if provided, otherwise falls back to hardcoded pricing lookup
  • shouldAutoCompact(messages, model, state, contextWindowSize?): Passes contextWindowSize through to getAutoCompactThreshold

Call Sites Updated

  • engine.ts: shouldAutoCompact and estimateCost calls now pass config values
  • agent.ts: QueryEngine construction passes contextWindowSize and pricingPerMillion from AgentOptions

Usage Examples

// Specify context window and pricing at agent creation
const agent = createAgent({
  model: 'custom-model',
  contextWindowSize: 500000,
  pricingPerMillion: { input: 5, output: 25 },
})

// Override at query time (takes precedence over agent-level config)
for await (const event of agent.query('hello', {
  contextWindowSize: 300000,
  pricingPerMillion: { input: 2, output: 8 },
})) { ... }

Backward Compatibility

All new parameters are optional. When not provided, behavior is identical to before this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant