feat(sandbox): add LiteLLMCompaction capability sized via litellm#3409
Closed
scalabreseGD wants to merge 4 commits into
Closed
feat(sandbox): add LiteLLMCompaction capability sized via litellm#3409scalabreseGD wants to merge 4 commits into
scalabreseGD wants to merge 4 commits into
Conversation
Add `agents.extensions.sandbox.LiteLLMCompaction`, a drop-in subclass of `agents.sandbox.capabilities.Compaction` whose default policy is sized from `litellm.get_model_info()` rather than the OpenAI-only context-window registry. The default `Compaction` registry catalogues OpenAI families only, so litellm-routed models (Anthropic, Bedrock, Vertex, custom proxy aliases, ...) miss the lookup and fall back to a hard-coded 240k `StaticCompactionPolicy` regardless of the model's actual input window — compacting a 1M-context model at the same point as a 200k model. `LiteLLMCompaction` exposes two classmethods: - `for_model(model, *, threshold=0.8)` resolves the cap via `litellm.get_model_info()`. Falls back to a 200k window with a WARNING when litellm has no entry (new beta IDs, custom proxy aliases, Bedrock embedding entries with no `max_input_tokens`). - `for_context_window(context_window, *, threshold=0.8)` builds the same policy from an explicitly-provided cap, for callers whose config layer already applies per-org ceilings or test overrides. Purely additive — no released APIs change. Gated behind the existing `openai-agents[litellm]` optional extra via `raise_optional_dependency_error`, and exported from `agents.extensions.sandbox` only when the extra is installed.
Add `agents.extensions.sandbox.LiteLLMCompaction`, a drop-in subclass of `agents.sandbox.capabilities.Compaction` whose default policy is sized from `litellm.get_model_info()` rather than the OpenAI-only context-window registry. The default `Compaction` registry catalogues OpenAI families only, so litellm-routed models (Anthropic, Bedrock, Vertex, custom proxy aliases, ...) miss the lookup and fall back to a hard-coded 240k `StaticCompactionPolicy` regardless of the model's actual input window — compacting a 1M-context model at the same point as a 200k model. `LiteLLMCompaction` exposes two classmethods: - `for_model(model, *, threshold=0.8)` resolves the cap via `litellm.get_model_info()`. Falls back to a 200k window with a WARNING when litellm has no entry (new beta IDs, custom proxy aliases, Bedrock embedding entries with no `max_input_tokens`). - `for_context_window(context_window, *, threshold=0.8)` builds the same policy from an explicitly-provided cap, for callers whose config layer already applies per-org ceilings or test overrides. Purely additive — no released APIs change. Gated behind the existing `openai-agents[litellm]` optional extra via `raise_optional_dependency_error`, and exported from `agents.extensions.sandbox` only when the extra is installed.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 21a06c23ac
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Member
|
Thanks for suggesting this. However, we don't plan to add this module within this SDK. If you need it for your projects, please feel free to share the code as your own package/module. |
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.
feat(sandbox): add LiteLLMCompaction capability sized via litellm
Summary
Adds
agents.extensions.sandbox.LiteLLMCompaction, a drop-in subclass ofagents.sandbox.capabilities.Compactionwhose default policy is sized fromlitellm.get_model_info()rather than the built-in OpenAI-only context-window registry.Why
The existing
Compactioncapability ships a private_MODEL_CONTEXT_WINDOWSregistry that catalogues OpenAI families only. When a litellm-routed model (Anthropic, Bedrock, Vertex, custom proxy aliases, ...) is used:CompactionModelInfo.maybe_for_model(model)misses the lookup.Compaction.sampling_paramstherefore cannot pick aDynamicCompactionPolicyand falls back to a hard-coded 240kStaticCompactionPolicy.What this PR adds
A new optional capability under the existing
openai-agents[litellm]extra:LiteLLMCompaction.for_model(model, *, threshold=0.8)— resolves the cap vialitellm.get_model_info()and builds aDynamicCompactionPolicysized to the reportedmax_input_tokens. Falls back to a conservative 200k window with aWARNINGwhen litellm has no entry for the model (brand-new beta IDs, custom proxy aliases, Bedrock embedding entries with nomax_input_tokens), so the run still benefits from compaction instead of failing.LiteLLMCompaction.for_context_window(context_window, *, threshold=0.8)— builds the same policy from an explicitly-provided cap, for callers whose config layer already applies per-org ceilings or test overrides that should clamp the compaction threshold below the litellm-reported window.The class is a pure pydantic subclass of
Compaction, soprocess_context,sampling_params, thetype: "compaction"serialization tag, and directLiteLLMCompaction(policy=...)construction all behave identically to the parent.Compatibility
Purely additive — no existing released APIs change. The module is gated behind the existing
openai-agents[litellm]optional extra viaraise_optional_dependency_error(same pattern asRedisSession,DaprSession,MongoDBSession), and is exported fromagents.extensions.sandbox.__init__only when the extra is installed.Docs
docs/ref/extensions/sandbox/litellm_compaction.md, registered underExtensions > Sandbox capabilities > LiteLLM compactioninmkdocs.yml.docs/sandbox/guide.mdnow calls out the OpenAI-only registry limitation right after the built-in capabilities table and points readers atLiteLLMCompactionfor litellm-routed deployments.Translated docs under
docs/ja/,docs/ko/, anddocs/zh/are intentionally untouched (they are generated).Usage
Test plan
New tests at
tests/extensions/sandbox/test_litellm_compaction.py(8 tests, all passing locally) cover:for_context_windowbuilds the rightDynamicCompactionPolicywith the default (0.8) and custom thresholds.sampling_paramsemits the correctcompact_thresholdend-to-end (context_window * threshold).for_modelcallslitellm.get_model_infoand usesmax_input_tokens.for_modelfalls back to the 200k default both when litellm raises and whenmax_input_tokensisNone(Bedrock-embedding-style entries).LiteLLMCompaction(policy=StaticCompactionPolicy(...))construction still works (parent contract preserved).make format,make lint, andmypyon the touched files all pass cleanly. The pre-existing mypy/pyright errors infunction_schema.py,openai_responses.py,mount_lifecycle.py, andexamples/tools/computer_use.pyreproduce onmainand are unrelated to this change.Issue number
N/A
Checks
make lintandmake format