Repository navigation
Prompt Compression - add it to the proxy #25729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
krrish-berri-2
merged 17 commits into
litellm_internal_staging
from
litellm_dev_04_14_2026_p1
Apr 20, 2026
Merged
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9c20b8c
refactor: new agentic loop event hook
krrish-berri-2 d1b9036
fix: compress - make it work on anthropic input as well
krrish-berri-2 91436a6
fix(compress.py): working prompt compression for claude code
krrish-berri-2 2e47b7f
docs: add agentic loop hook guide
krrish-berri-2 154b445
docs: add agentic_loop_hook to sidebar
krrish-berri-2 6dfad8b
Merge branch 'main' into litellm_dev_04_14_2026_p1
krrish-berri-2 df0fdb7
fix: fix multiple arguments error
krrish-berri-2 e23707d
fix: fix tool call loop for compression on streaming /v1/messages
krrish-berri-2 e80f12b
fix: fix linting errors
krrish-berri-2 11e22bd
fix: fix ci/cd errors
krrish-berri-2 37d6a18
feat(litellm_pre_call_utils.py): use claude code session for litellm …
krrish-berri-2 7dd39d4
fix: suppress incorrect mypy warning rE: module
krrish-berri-2 0deb071
revert: drop PR's changes to litellm/proxy/_experimental/out/
krrish-berri-2 f123e55
fix: address greptile review comments on PR #25729
krrish-berri-2 a1a7312
revert: drop LITELLM_CAPTURE_VENDOR_SESSION_HEADERS flag
krrish-berri-2 0eade56
refactor(compress): replace input_type with CallTypes call_type
krrish-berri-2 401ac41
Merge branch 'litellm_internal_staging' into litellm_dev_04_14_2026_p1
krrish-berri-2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Agentic Loop Hook | ||
|
|
||
| Build a `CustomLogger` callback that intercepts a model response, fulfills tool calls server-side, and reruns the model — transparently to the caller. | ||
|
|
||
| :::info Supported call types | ||
| - `async` only (sync calls do not trigger the hook) | ||
| - Non-streaming only (streaming responses cannot be inspected for tool calls) | ||
| - Works on both `/v1/messages` and `/v1/chat/completions` | ||
| ::: | ||
|
|
||
| ## Implement the callback | ||
|
|
||
| Override two methods on `CustomLogger`: | ||
|
|
||
| ```python | ||
| from litellm.integrations.custom_logger import CustomLogger | ||
| from litellm.types.integrations.custom_logger import AgenticLoopPlan, AgenticLoopRequestPatch | ||
|
|
||
| MY_TOOL = "my_tool" | ||
|
|
||
| class MyToolCallback(CustomLogger): | ||
|
|
||
| async def async_should_run_agentic_loop( | ||
| self, response, model, messages, tools, stream, custom_llm_provider, kwargs | ||
| ): | ||
| # Return (True, context_dict) if there are tool calls to handle | ||
| content = getattr(response, "content", None) or [] | ||
| calls = [b for b in content if isinstance(b, dict) | ||
| and b.get("type") == "tool_use" and b.get("name") == MY_TOOL] | ||
| if not calls: | ||
| return False, {} | ||
| return True, {"tool_calls": calls} | ||
|
|
||
| async def async_build_agentic_loop_plan( | ||
| self, tools, model, messages, response, | ||
| anthropic_messages_provider_config, | ||
| anthropic_messages_optional_request_params, | ||
| logging_obj, stream, kwargs, | ||
| ): | ||
| calls = tools["tool_calls"] | ||
| results = [f"result for {c['input']}" for c in calls] # your logic here | ||
|
|
||
| follow_up = messages + [ | ||
| {"role": "assistant", "content": [ | ||
| {"type": "tool_use", "id": c["id"], "name": c["name"], "input": c["input"]} | ||
| for c in calls | ||
| ]}, | ||
| {"role": "user", "content": [ | ||
| {"type": "tool_result", "tool_use_id": c["id"], "content": results[i]} | ||
| for i, c in enumerate(calls) | ||
| ]}, | ||
| ] | ||
| return AgenticLoopPlan( | ||
| run_agentic_loop=True, | ||
| request_patch=AgenticLoopRequestPatch(messages=follow_up), | ||
| ) | ||
| ``` | ||
|
|
||
| For `/v1/chat/completions`, override `async_build_chat_completion_agentic_loop_plan` instead — same idea, `optional_params` replaces `anthropic_messages_optional_request_params`. | ||
|
|
||
| ## Register it | ||
|
|
||
| ```python | ||
| import litellm | ||
| litellm.callbacks = [MyToolCallback()] | ||
| ``` | ||
|
|
||
| Or in `config.yaml`: | ||
|
|
||
| ```yaml | ||
| litellm_settings: | ||
| callbacks: ["my_module.MyToolCallback"] | ||
| ``` | ||
|
|
||
| ## `AgenticLoopPlan` fields | ||
|
|
||
| | Field | Effect | | ||
| |---|---| | ||
| | `run_agentic_loop=True` + `request_patch` | Reruns the model with the patched request | | ||
| | `response_override` | Returns this value directly to the caller (no rerun) | | ||
| | `terminate=True` | Stops the loop, returns the current response | | ||
| | `run_agentic_loop=False` (default) | Skips; next callback is checked | | ||
|
|
||
| `AgenticLoopRequestPatch` accepts: `model`, `messages`, `tools`, `max_tokens`, `optional_params`, `kwargs`. | ||
|
|
||
| ## Loop safety | ||
|
|
||
| - Default max reruns: `3` — override per-request with `kwargs["max_agentic_loops"]` | ||
| - Identical tool-call fingerprints abort the loop automatically | ||
| - Current depth is in `kwargs["_agentic_loop_depth"]` | ||
|
|
||
| ## Examples in this repo | ||
|
|
||
| - `litellm/integrations/compression_interception/handler.py` | ||
| - `litellm/integrations/websearch_interception/handler.py` |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why introduce a new input_type ? We already have call types completion, messages etc