Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ad9aa43
chore(proxy): scope skills and container resources
stuxf May 1, 2026
95c8406
chore(proxy): stabilize openapi snapshot ids
stuxf May 1, 2026
d2a3220
fix(proxy): preserve container ownership compatibility
stuxf May 1, 2026
0745b18
test(proxy): cover container endpoint forwarding
stuxf May 1, 2026
e4741fb
fix(proxy): add legacy skill ownership opt-out
stuxf May 1, 2026
9376b30
fix(proxy): reset filtered container pagination
stuxf May 1, 2026
6aac455
fix(proxy): forward decoded container ids
stuxf May 1, 2026
f3cff93
fix(proxy): harden resource ownership fallbacks
stuxf May 1, 2026
7b1e3f2
test(proxy): cover container endpoint post processing
stuxf May 1, 2026
20eb9c9
fix(proxy): avoid mutating container responses
stuxf May 1, 2026
3a566c3
fix(proxy): stabilize ownership fallback and openapi ids
stuxf May 1, 2026
a02aacf
fix(proxy): type openapi snapshot operation ids
stuxf May 1, 2026
2ecc79b
test(proxy): cover skill ownership propagation
stuxf May 1, 2026
f18ee03
fix(proxy): isolate ownership persistence paths
stuxf May 1, 2026
e9fb89b
fix(proxy): avoid misleading multi-method operation ids
stuxf May 1, 2026
8ced8d2
Merge remote-tracking branch 'origin/litellm_internal_staging' into HEAD
stuxf May 1, 2026
d812a53
fix managed container routing after staging merge
stuxf May 1, 2026
6c37e9d
fix(proxy): handle ownership-recording failures after upstream create
stuxf May 2, 2026
22ced8d
Merge upstream/litellm_internal_staging into HEAD
stuxf May 2, 2026
6194028
perf(proxy): cache container/skill ownership reads on the hot path
stuxf May 2, 2026
c01f209
fix(proxy): forward decoded container ids after ownership checks
stuxf May 2, 2026
4fa5778
fix(container): keep ownership-filter exceptions out of the LLM-error…
stuxf May 4, 2026
ec9b84d
chore(container,skills): LRU eviction for owner caches; widen file_pu…
stuxf May 4, 2026
de682c8
chore(container,skills): drop legacy-access opt-out env vars
stuxf May 4, 2026
758b488
fix(ownership): reject identity-less callers instead of sharing a sen…
stuxf May 4, 2026
777862a
Merge remote-tracking branch 'upstream/litellm_internal_staging' into…
stuxf May 4, 2026
b5a14f2
Merge remote-tracking branch 'upstream/litellm_internal_staging' into…
stuxf May 4, 2026
12fe945
fix: keep skills handler FastAPI-free; fold gcs deny list into the bo…
stuxf May 4, 2026
6ce84ef
chore: simplify ownership tracking — drop thin stores, in-memory fall…
stuxf May 5, 2026
2adfa96
fix(container): cache list-allow-set, track admin-created containers
stuxf May 5, 2026
4699b3d
chore(container): use delete_cache, json-encode scope key, clean test
stuxf May 5, 2026
3dcb6bd
Merge remote-tracking branch 'upstream/litellm_internal_staging' into…
stuxf May 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 139 additions & 23 deletions litellm/llms/litellm_proxy/skills/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,69 @@
Used by the transformation layer and skills injection hook.
"""

import time
import uuid
from typing import Any, Dict, List, Optional
from collections import OrderedDict
from typing import Any, Dict, List, Optional, Tuple

from litellm._logging import verbose_logger
from litellm.proxy._types import LiteLLM_SkillsTable, NewSkillRequest
from litellm.llms.litellm_proxy.skills.store import LiteLLMSkillsStore
from litellm.proxy._types import LiteLLM_SkillsTable, NewSkillRequest, UserAPIKeyAuth
from litellm.proxy.common_utils.resource_ownership import (
get_primary_resource_owner_scope,
get_resource_owner_scopes,
is_proxy_admin,
user_can_access_resource_owner,
)

# Skills are looked up on every chat completion that has skills enabled
# (`LiteLLMSkillsHandler.fetch_skill_from_db` in the injection hook). Cache
# the Prisma skill row for a short window so the hot path doesn't issue a DB
# round-trip per request. Same shape as `_byok_cred_cache` and the container
# ownership cache: (value, monotonic_timestamp). `None` is cached as a true
# negative ("skill does not exist") so repeated misses also avoid the DB.
_SKILL_CACHE: "OrderedDict[str, Tuple[Optional[Any], float]]" = OrderedDict()
_SKILL_CACHE_TTL = 60 # seconds
_SKILL_CACHE_MAX_SIZE = 10000


def _read_skill_cache(skill_id: str) -> Tuple[bool, Optional[Any]]:
"""Return (hit, value). hit=False means caller must consult the DB."""
entry = _SKILL_CACHE.get(skill_id)
if entry is None:
return False, None
value, timestamp = entry
if time.monotonic() - timestamp > _SKILL_CACHE_TTL:
_SKILL_CACHE.pop(skill_id, None)
return False, None
_SKILL_CACHE.move_to_end(skill_id)
return True, value


def _write_skill_cache(skill_id: str, skill: Optional[Any]) -> None:
# LRU eviction (popitem(last=False)) instead of full ``clear()`` —
# see container ownership cache for rationale.
if skill_id in _SKILL_CACHE:
_SKILL_CACHE.move_to_end(skill_id)
_SKILL_CACHE[skill_id] = (skill, time.monotonic())
while len(_SKILL_CACHE) > _SKILL_CACHE_MAX_SIZE:
_SKILL_CACHE.popitem(last=False)


def _invalidate_skill_cache(skill_id: str) -> None:
"""Drop the cache entry after a write so the next read sees the new row."""
_SKILL_CACHE.pop(skill_id, None)


def _user_can_access_skill_owner(
owner: Optional[str],
user_api_key_dict: Optional[UserAPIKeyAuth],
) -> bool:
# Pre-isolation skills with no ``created_by`` are admin-only — same
# rule as untracked containers. Owners need to either re-create via
# the now-tracked flow or have an admin assign ``created_by`` on the
# row.
return user_can_access_resource_owner(owner, user_api_key_dict)


def _prisma_skill_to_litellm(prisma_skill) -> LiteLLM_SkillsTable:
Expand Down Expand Up @@ -58,6 +116,7 @@ async def _get_prisma_client():
async def create_skill(
data: NewSkillRequest,
user_id: Optional[str] = None,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
) -> LiteLLM_SkillsTable:
"""
Create a new skill in the LiteLLM database.
Expand All @@ -70,17 +129,29 @@ async def create_skill(
LiteLLM_SkillsTable record
"""
prisma_client = await LiteLLMSkillsHandler._get_prisma_client()
store = LiteLLMSkillsStore(prisma_client)

skill_id = f"litellm_skill_{uuid.uuid4()}"
owner = get_primary_resource_owner_scope(user_api_key_dict) or user_id
if owner is None:
# Caller has no identity scope (no user_id / team_id / org_id /
# api_key / token). Stamping a placeholder would let any two
# identity-less callers see each other's skills via the shared
# owner — the cross-tenant primitive we avoid. ValueError keeps
# this module FastAPI-free per the project layering rule
# (litellm_proxy provider integrations live outside proxy/).
raise ValueError(
"Unable to record skill ownership: caller has no identity scope."
)

skill_data: Dict[str, Any] = {
"skill_id": skill_id,
"display_title": data.display_title,
"description": data.description,
"instructions": data.instructions,
"source": "custom",
"created_by": user_id,
"updated_by": user_id,
"created_by": owner,
"updated_by": owner,
}

# Handle metadata
Expand All @@ -103,14 +174,15 @@ async def create_skill(
f"LiteLLMSkillsHandler: Creating skill {skill_id} with title={data.display_title}"
)

new_skill = await prisma_client.db.litellm_skillstable.create(data=skill_data)
new_skill = await store.create_skill(skill_data)

return _prisma_skill_to_litellm(new_skill)

@staticmethod
async def list_skills(
limit: int = 20,
offset: int = 0,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
) -> List[LiteLLM_SkillsTable]:
"""
List skills from the LiteLLM database.
Expand All @@ -123,21 +195,50 @@ async def list_skills(
List of LiteLLM_SkillsTable records
"""
prisma_client = await LiteLLMSkillsHandler._get_prisma_client()
store = LiteLLMSkillsStore(prisma_client)

verbose_logger.debug(
f"LiteLLMSkillsHandler: Listing skills with limit={limit}, offset={offset}"
)

skills = await prisma_client.db.litellm_skillstable.find_many(
take=limit,
skip=offset,
order={"created_at": "desc"},
)
find_many_kwargs: Dict[str, Any] = {
"take": limit,
"skip": offset,
"order": {"created_at": "desc"},
}
if user_api_key_dict is not None and not is_proxy_admin(user_api_key_dict):
owner_scopes = get_resource_owner_scopes(user_api_key_dict)
if not owner_scopes:
return []
find_many_kwargs["where"] = {"created_by": {"in": owner_scopes}}

skills = await store.list_skills(find_many_kwargs)

return [_prisma_skill_to_litellm(s) for s in skills]

@staticmethod
async def get_skill(skill_id: str) -> LiteLLM_SkillsTable:
async def _load_skill(skill_id: str) -> Optional[Any]:
"""Cache-first read of the Prisma skill row.

Caching here keeps `fetch_skill_from_db` (called per chat completion in
the skills injection hook) off the DB. Owner-scope filtering happens
on the cached row, so the cache is per-skill and not per-caller.
"""
cached_hit, cached_skill = _read_skill_cache(skill_id)
if cached_hit:
return cached_skill

prisma_client = await LiteLLMSkillsHandler._get_prisma_client()
store = LiteLLMSkillsStore(prisma_client)
skill = await store.find_skill(skill_id)
_write_skill_cache(skill_id, skill)
return skill

@staticmethod
async def get_skill(
skill_id: str,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
) -> LiteLLM_SkillsTable:
"""
Get a skill by ID from the LiteLLM database.

Expand All @@ -150,21 +251,25 @@ async def get_skill(skill_id: str) -> LiteLLM_SkillsTable:
Raises:
ValueError: If skill not found
"""
prisma_client = await LiteLLMSkillsHandler._get_prisma_client()

verbose_logger.debug(f"LiteLLMSkillsHandler: Getting skill {skill_id}")

skill = await prisma_client.db.litellm_skillstable.find_unique(
where={"skill_id": skill_id}
)
skill = await LiteLLMSkillsHandler._load_skill(skill_id)

if skill is None:
raise ValueError(f"Skill not found: {skill_id}")

if not _user_can_access_skill_owner(
getattr(skill, "created_by", None), user_api_key_dict
):
raise ValueError(f"Skill not found: {skill_id}")
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

return _prisma_skill_to_litellm(skill)

@staticmethod
async def delete_skill(skill_id: str) -> Dict[str, str]:
async def delete_skill(
skill_id: str,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
) -> Dict[str, str]:
"""
Delete a skill by ID from the LiteLLM database.

Expand All @@ -178,24 +283,32 @@ async def delete_skill(skill_id: str) -> Dict[str, str]:
ValueError: If skill not found
"""
prisma_client = await LiteLLMSkillsHandler._get_prisma_client()
store = LiteLLMSkillsStore(prisma_client)

verbose_logger.debug(f"LiteLLMSkillsHandler: Deleting skill {skill_id}")

# Check if skill exists
skill = await prisma_client.db.litellm_skillstable.find_unique(
where={"skill_id": skill_id}
)
skill = await LiteLLMSkillsHandler._load_skill(skill_id)

if skill is None:
raise ValueError(f"Skill not found: {skill_id}")

if not _user_can_access_skill_owner(
getattr(skill, "created_by", None), user_api_key_dict
):
raise ValueError(f"Skill not found: {skill_id}")

# Delete the skill
await prisma_client.db.litellm_skillstable.delete(where={"skill_id": skill_id})
await store.delete_skill(skill_id)
_invalidate_skill_cache(skill_id)

return {"id": skill_id, "type": "skill_deleted"}

@staticmethod
async def fetch_skill_from_db(skill_id: str) -> Optional[LiteLLM_SkillsTable]:
async def fetch_skill_from_db(
skill_id: str,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
) -> Optional[LiteLLM_SkillsTable]:
"""
Fetch a skill from the database (used by skills injection hook).

Expand All @@ -209,7 +322,10 @@ async def fetch_skill_from_db(skill_id: str) -> Optional[LiteLLM_SkillsTable]:
LiteLLM_SkillsTable or None if not found
"""
try:
return await LiteLLMSkillsHandler.get_skill(skill_id)
return await LiteLLMSkillsHandler.get_skill(
skill_id,
user_api_key_dict=user_api_key_dict,
)
except ValueError:
return None
except Exception as e:
Expand Down
22 changes: 22 additions & 0 deletions litellm/llms/litellm_proxy/skills/store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Any, Dict, List, Optional


class LiteLLMSkillsStore:
def __init__(self, prisma_client: Any):
self.prisma_client = prisma_client

@property
def _table(self) -> Any:
return self.prisma_client.db.litellm_skillstable

async def create_skill(self, data: Dict[str, Any]) -> Any:
return await self._table.create(data=data)

async def list_skills(self, find_many_kwargs: Dict[str, Any]) -> List[Any]:
return await self._table.find_many(**find_many_kwargs)

async def find_skill(self, skill_id: str) -> Optional[Any]:
return await self._table.find_unique(where={"skill_id": skill_id})

async def delete_skill(self, skill_id: str) -> None:
await self._table.delete(where={"skill_id": skill_id})
Loading
Loading