Skip to content

feat: add context window limit lookup table#2249

Merged
opieter-aws merged 1 commit intostrands-agents:mainfrom
opieter-aws:feat/context-window-limit-lookup
May 5, 2026
Merged

feat: add context window limit lookup table#2249
opieter-aws merged 1 commit intostrands-agents:mainfrom
opieter-aws:feat/context-window-limit-lookup

Conversation

@opieter-aws
Copy link
Copy Markdown
Contributor

@opieter-aws opieter-aws commented May 5, 2026

Description

context_window_limit is required for proactive context compression (#555, #1295), but users currently have to manually look up and hard-code context window sizes for every model they instantiate. This is error-prone and creates friction for the most common models across all providers.

context_window_limit is now auto-populated from a built-in lookup table when not explicitly provided in the model config. Explicit values always take precedence. Unknown model IDs leave it None.

# Before: users had to supply context_window_limit manually
model = BedrockModel(
    model_id="anthropic.claude-sonnet-4-20250514-v1:0",
    context_window_limit=1_000_000,
)

# After: resolved automatically from the model ID
model = BedrockModel(model_id="anthropic.claude-sonnet-4-20250514-v1:0")
model.get_config().get("context_window_limit")  # 1_000_000

A new _defaults.py module maps model IDs to their known context window limits. Each provider's get_config() lazily resolves the limit via resolve_config_metadata(). The stored config is never
mutated, so changing model_id via update_config() naturally re-resolves on the next get_config() call.

For Bedrock cross-region model IDs (e.g. us.anthropic.claude-sonnet-4-6), the region prefix is stripped before lookup.

Values sourced from https://github.com/BerriAI/litellm/blob/litellm_internal_staging/model_prices_and_context_window.json

Related Issues

Port of strands-agents/sdk-typescript#954.

Resolves: #1295

Documentation PR

Part of proactive compression doc updates

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread src/strands/models/_defaults.py Outdated
Comment thread src/strands/models/_defaults.py Outdated
Comment thread src/strands/models/_defaults.py
Comment thread src/strands/models/_defaults.py
Comment thread src/strands/models/bedrock.py
Comment thread src/strands/models/_defaults.py Outdated
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Assessment: Comment

The design is sound — lazy resolution in get_config() without mutating stored state is a clean approach that handles update_config() naturally. The main areas for improvement are around type safety, consistency across providers, and allocation patterns on hot paths.

Review Categories
  • Type Safety: resolve_config_metadata erases TypedDict type information; a generic type parameter would preserve it.
  • Consistency: Only 5 of 13 model providers get auto-resolution; users of LiteLLM/Ollama/Mistral/etc. won't benefit.
  • Performance: get_config() may allocate a new dict on every call for known models — consider caching the resolved result.
  • Maintainability: The static lookup table needs a documented update process to avoid staleness.
  • Edge Cases: The is not None check and GovCloud prefix format deserve verification.

Good feature that directly reduces friction for the most common use case.

@opieter-aws opieter-aws force-pushed the feat/context-window-limit-lookup branch from 0557068 to 46091e3 Compare May 5, 2026 12:51
@github-actions github-actions Bot added size/m and removed size/m labels May 5, 2026
@opieter-aws opieter-aws force-pushed the feat/context-window-limit-lookup branch from 46091e3 to 6def016 Compare May 5, 2026 12:59
@github-actions github-actions Bot added size/m and removed size/m labels May 5, 2026
@opieter-aws opieter-aws temporarily deployed to manual-approval May 5, 2026 12:59 — with GitHub Actions Inactive
Comment thread src/strands/models/_defaults.py
Comment thread src/strands/models/openai.py
Comment thread src/strands/models/openai_responses.py
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Assessment: Comment (trending toward Approve)

The update addressed the key concerns from the previous review: type safety via TypeVar, presence check via in, Mistral coverage, and simpler prefix-stripping logic. The remaining issues are minor.

Remaining Items
  • Performance: Dict allocation on every get_config() call for known models — consider caching (non-blocking)
  • Consistency: Remaining providers (Ollama, LiteLLM, Writer, etc.) are excluded — a brief code comment explaining why would help
  • Minor cleanup: Unnecessary str() wrappers in openai.py and openai_responses.py
  • Observability: Debug-level logging when prefix-stripping fires would aid troubleshooting

None of these are blocking. The core design is solid and well-tested.

@opieter-aws opieter-aws force-pushed the feat/context-window-limit-lookup branch from 6def016 to 1ccc5a4 Compare May 5, 2026 13:09
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Assessment: Approve

All substantive feedback from previous rounds has been addressed. The code is clean, well-tested (100% coverage on modified lines), and the design decisions are well-documented in the module docstring.

Resolved Items
  • ✅ Provider exclusion rationale documented in module docstring
  • ✅ Type safety handled via cast() at each call site
  • ✅ Presence check uses "context_window_limit" in config (not is not None)
  • ✅ Mistral provider added
  • str() wrapper confirmed required by mypy
Non-blocking Suggestions (still open)
  • Performance: Consider caching the resolved config to avoid dict allocation on every get_config() call for known models. Not a correctness issue — purely optimization.
  • Observability: A debug-level log when the prefix-stripping fallback fires would help troubleshooting.

Well-designed feature that removes real user friction. Ship it! 🚀

@opieter-aws opieter-aws force-pushed the feat/context-window-limit-lookup branch from 1ccc5a4 to abbd94b Compare May 5, 2026 13:37
@github-actions github-actions Bot added size/m and removed size/m labels May 5, 2026
@opieter-aws opieter-aws temporarily deployed to manual-approval May 5, 2026 13:37 — with GitHub Actions Inactive
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Assessment: Approve

All feedback from previous review rounds has been addressed. This iteration brings a clean TypeVar with Mapping[str, object] bound that properly preserves TypedDict types without needing casts (except where they were pre-existing in OpenAI providers), plus debug-level logging for the prefix-stripping fallback.

The code is well-structured, well-tested, well-documented, and follows the project's coding standards. No remaining blocking concerns.

@opieter-aws opieter-aws marked this pull request as ready for review May 5, 2026 13:46
Comment thread src/strands/models/_defaults.py
@opieter-aws opieter-aws merged commit 559b2a0 into strands-agents:main May 5, 2026
34 of 36 checks passed
@opieter-aws opieter-aws deleted the feat/context-window-limit-lookup branch May 5, 2026 15:44
Comment thread src/strands/models/_defaults.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CONTEXT] [FEATURE] Context Limit Property on Model Interface

2 participants