-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
feat(proxy): tool policies - auto-discover tools + policy enforcement guardrail #22041
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83c7677
feat(proxy): tool policies - auto-discover tools, manage policies, guβ¦
ishaan-jaff 2d28f21
feat(tool-policies): track call_count + discover tools from request bβ¦
ishaan-jaff ab46c1b
fix: address greptile review feedback
ishaan-jaff c5d4977
fix: address greptile review round 2
ishaan-jaff c197fe9
fix: address greptile review round 3
ishaan-jaff d4fac55
fix: cache tool policies per tool name not per combination
ishaan-jaff 43b9a2a
Update ui/litellm-dashboard/src/components/ToolPolicies.tsx
ishaan-jaff 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
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
54 changes: 54 additions & 0 deletions
54
litellm/proxy/db/db_transaction_queue/tool_discovery_queue.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| In-memory buffer for tool registry upserts. | ||
|
|
||
| Unlike SpendUpdateQueue (which aggregates increments), ToolDiscoveryQueue | ||
| uses set-deduplication: each unique tool_name is only queued once per flush | ||
| cycle (~30s). The seen-set is cleared on every flush so that call_count | ||
| increments in subsequent cycles rather than stopping after the first flush. | ||
| """ | ||
|
|
||
| from typing import List, Set | ||
|
|
||
| from litellm._logging import verbose_proxy_logger | ||
| from litellm.proxy._types import ToolDiscoveryQueueItem | ||
|
|
||
|
|
||
| class ToolDiscoveryQueue: | ||
| """ | ||
| In-memory buffer for tool registry upserts. | ||
|
|
||
| Deduplicates by tool_name within each flush cycle: a tool is only queued | ||
| once per ~30s batch, so call_count increments once per flush cycle the | ||
| tool appears in (not once per invocation, but not once per pod lifetime | ||
| either). The seen-set is cleared on flush so subsequent batches can | ||
| re-count the same tool. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._seen_tool_names: Set[str] = set() | ||
| self._pending: List[ToolDiscoveryQueueItem] = [] | ||
|
|
||
| def add_update(self, item: ToolDiscoveryQueueItem) -> None: | ||
| """Enqueue a tool discovery item if tool_name has not been seen before.""" | ||
| tool_name = item.get("tool_name", "") | ||
| if not tool_name: | ||
| return | ||
| if tool_name in self._seen_tool_names: | ||
| verbose_proxy_logger.debug( | ||
| "ToolDiscoveryQueue: skipping already-seen tool %s", tool_name | ||
| ) | ||
| return | ||
| self._seen_tool_names.add(tool_name) | ||
| self._pending.append(item) | ||
| verbose_proxy_logger.debug( | ||
| "ToolDiscoveryQueue: queued new tool %s (origin=%s)", | ||
| tool_name, | ||
| item.get("origin"), | ||
| ) | ||
|
|
||
| def flush(self) -> List[ToolDiscoveryQueueItem]: | ||
| """Return and clear all pending items. Resets seen-set so the next | ||
| flush cycle can re-count the same tools.""" | ||
| items, self._pending = self._pending, [] | ||
| self._seen_tool_names.clear() | ||
| return items | ||
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.
Module docstring contradicts class docstring and implementation
The module-level docstring (lines 1β7) says "each unique tool_name is only queued once per pod lifetime, so DB upserts stop entirely after warmup." However, the class docstring (lines 17β24) says "The seen-set is cleared on flush so subsequent batches can re-count the same tool," and
flush()at line 52 indeed callsself._seen_tool_names.clear().This inconsistency also causes the test
test_seen_names_persist_across_flushesto fail (see related comment). Please update the module docstring to reflect the actual per-flush-cycle dedup behavior.