Skip to content
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

feat: add GET route to get he breakdown of an agent's context window #1889

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions letta/schemas/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
from letta.schemas.block import Block


class ContextWindowOverview(BaseModel):
"""
Overview of the context window, including the number of messages and tokens.
"""

num_messages: int = Field(..., description="The number of messages in the context window.")
num_tokens: int = Field(..., description="The number of tokens in the context window.")


class Memory(BaseModel, validate_assignment=True):
"""

Expand Down
15 changes: 15 additions & 0 deletions letta/server/rest_api/routers/v1/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from letta.schemas.memory import (
ArchivalMemorySummary,
BasicBlockMemory,
ContextWindowOverview,
CreateArchivalMemory,
Memory,
RecallMemorySummary,
Expand Down Expand Up @@ -51,6 +52,20 @@ def list_agents(
return server.list_agents(user_id=actor.id)


@router.get("/{agent_id}/context", response_model=ContextWindowOverview, operation_id="get_agent_context_window")
def get_agent_context_window(
agent_id: str,
server: "SyncServer" = Depends(get_letta_server),
user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
):
"""
Retrieve the context window of a specific agent.
"""
actor = server.get_user_or_default(user_id=user_id)

return server.get_agent_context_window(user_id=actor.id, agent_id=agent_id)


@router.post("/", response_model=AgentState, operation_id="create_agent")
def create_agent(
agent: CreateAgent = Body(...),
Expand Down
14 changes: 13 additions & 1 deletion letta/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@
from letta.schemas.job import Job
from letta.schemas.letta_message import LettaMessage
from letta.schemas.llm_config import LLMConfig
from letta.schemas.memory import ArchivalMemorySummary, Memory, RecallMemorySummary
from letta.schemas.memory import (
ArchivalMemorySummary,
ContextWindowOverview,
Memory,
RecallMemorySummary,
)
from letta.schemas.message import Message, MessageCreate, MessageRole, UpdateMessage
from letta.schemas.openai.chat_completion_response import UsageStatistics
from letta.schemas.organization import Organization, OrganizationCreate
Expand Down Expand Up @@ -2087,3 +2092,10 @@ def add_llm_model(self, request: LLMConfig) -> LLMConfig:

def add_embedding_model(self, request: EmbeddingConfig) -> EmbeddingConfig:
"""Add a new embedding model"""

def get_agent_context_window(
self,
user_id: str,
agent_id: str,
) -> ContextWindowOverview:
raise NotImplementedError
Loading