Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: sleeptime billing context (#10082)
Kian Jones4e11aadc7df4
diff --git a/letta/groups/sleeptime_multi_agent_v2.py b/letta/groups/sleeptime_multi_agent_v2.pyindex e842e0b7f..1492b5299 100644--- a/letta/groups/sleeptime_multi_agent_v2.py+++ b/letta/groups/sleeptime_multi_agent_v2.py@@ -57,6 +57,7 @@ class SleeptimeMultiAgentV2(BaseAgent):self.step_manager = step_managerself.telemetry_manager = telemetry_managerself.current_run_id = current_run_id+ self._billing_context: BillingContext | None = None# Group settingsassert group.manager_type == ManagerType.sleeptime, f"Expected group manager type to be 'sleeptime', got {group.manager_type}"self.group = group@@ -73,6 +74,7 @@ class SleeptimeMultiAgentV2(BaseAgent):billing_context: "BillingContext | None" = None,) -> LettaResponse:run_ids = []+ self._billing_context = billing_context# Prepare new messagesnew_messages = []@@ -126,6 +128,7 @@ class SleeptimeMultiAgentV2(BaseAgent):last_response_messages,last_processed_message_id,use_assistant_message,+ billing_context,)run_ids.append(run_id)@@ -168,6 +171,7 @@ class SleeptimeMultiAgentV2(BaseAgent):use_assistant_message: bool = True,request_start_timestamp_ns: int | None = None,include_return_message_types: list[MessageType] | None = None,+ billing_context: BillingContext | None = None,) -> AsyncGenerator[str, None]:# Prepare new messagesnew_messages = []@@ -197,6 +201,7 @@ class SleeptimeMultiAgentV2(BaseAgent):use_assistant_message=use_assistant_message,request_start_timestamp_ns=request_start_timestamp_ns,include_return_message_types=include_return_message_types,+ billing_context=billing_context,):yield chunk@@ -220,6 +225,7 @@ class SleeptimeMultiAgentV2(BaseAgent):last_response_messages,last_processed_message_id,use_assistant_message,+ billing_context,)async def _issue_background_task(@@ -228,6 +234,7 @@ class SleeptimeMultiAgentV2(BaseAgent):response_messages: list[Message],last_processed_message_id: str,use_assistant_message: bool = True,+ billing_context: BillingContext | None = None,) -> str:run = Run(user_id=self.actor.id,@@ -247,6 +254,7 @@ class SleeptimeMultiAgentV2(BaseAgent):last_processed_message_id=last_processed_message_id,run_id=run.id,use_assistant_message=True,+ billing_context=billing_context,),label=f"participant_agent_step_{sleeptime_agent_id}",)@@ -260,6 +268,7 @@ class SleeptimeMultiAgentV2(BaseAgent):last_processed_message_id: str,run_id: str,use_assistant_message: bool = True,+ billing_context: BillingContext | None = None,) -> str:try:# Update job status@@ -315,6 +324,7 @@ class SleeptimeMultiAgentV2(BaseAgent):input_messages=sleeptime_agent_messages,use_assistant_message=use_assistant_message,run_id=run_id,+ billing_context=billing_context,)# Update job statusdiff --git a/letta/groups/sleeptime_multi_agent_v3.py b/letta/groups/sleeptime_multi_agent_v3.pyindex 4ddb2c08b..4be3504f8 100644--- a/letta/groups/sleeptime_multi_agent_v3.py+++ b/letta/groups/sleeptime_multi_agent_v3.py@@ -36,6 +36,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):# Additional manager classesself.group_manager = GroupManager()+ self._billing_context: BillingContext | None = None@trace_methodasync def step(@@ -52,6 +53,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):billing_context: "BillingContext | None" = None,) -> LettaResponse:self.run_ids = []+ self._billing_context = billing_contextfor i in range(len(input_messages)):input_messages[i].group_id = self.group.id@@ -69,7 +71,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):billing_context=billing_context,)- await self.run_sleeptime_agents()+ await self.run_sleeptime_agents(billing_context=billing_context)response.usage.run_ids = self.run_idsreturn response@@ -91,6 +93,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):openai_responses_websocket: bool = False,) -> AsyncGenerator[str, None]:self.run_ids = []+ self._billing_context = billing_contextfor i in range(len(input_messages)):input_messages[i].group_id = self.group.id@@ -114,13 +117,15 @@ class SleeptimeMultiAgentV3(LettaAgentV2):finally:# For some reason, stream is throwing a GeneratorExit even though it appears the that client# is getting the whole stream. This pattern should work to ensure sleeptime agents run despite this.- await self.run_sleeptime_agents()+ await self.run_sleeptime_agents(billing_context=billing_context)@trace_method- async def run_sleeptime_agents(self):+ async def run_sleeptime_agents(self, billing_context: BillingContext | None = None):# Get response messageslast_response_messages = self.response_messages+ billing_context = billing_context or self._billing_context+# Update turns counterturns_counter = Noneif self.group.sleeptime_agent_frequency is not None and self.group.sleeptime_agent_frequency > 0:@@ -139,6 +144,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):sleeptime_agent_id,last_response_messages,last_processed_message_id,+ billing_context,)self.run_ids.append(sleeptime_run_id)except Exception as e:@@ -152,6 +158,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):sleeptime_agent_id: str,response_messages: list[Message],last_processed_message_id: str,+ billing_context: BillingContext | None,) -> str:run = Run(agent_id=sleeptime_agent_id,@@ -170,6 +177,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):response_messages=response_messages,last_processed_message_id=last_processed_message_id,run_id=run.id,+ billing_context=billing_context,),label=f"participant_agent_step_{sleeptime_agent_id}",)@@ -183,6 +191,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):response_messages: list[Message],last_processed_message_id: str,run_id: str,+ billing_context: BillingContext | None,) -> LettaResponse:try:# Update run status@@ -227,6 +236,7 @@ class SleeptimeMultiAgentV3(LettaAgentV2):result = await sleeptime_agent.step(input_messages=sleeptime_agent_messages,run_id=run_id,+ billing_context=billing_context,)# Update run statusdiff --git a/letta/groups/sleeptime_multi_agent_v4.py b/letta/groups/sleeptime_multi_agent_v4.pyindex 8ee71388b..a2f8ecc56 100644--- a/letta/groups/sleeptime_multi_agent_v4.py+++ b/letta/groups/sleeptime_multi_agent_v4.py@@ -36,6 +36,10 @@ class SleeptimeMultiAgentV4(LettaAgentV3):# Additional manager classesself.group_manager = GroupManager()+ # Preserve the request billing context for follow-up sleeptime runs that are+ # triggered entirely within core and therefore never traverse cloud-api.+ self._billing_context: BillingContext | None = None+@trace_methodasync def step(self,@@ -52,6 +56,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):billing_context: "BillingContext | None" = None,) -> LettaResponse:self.run_ids = []+ self._billing_context = billing_contextfor i in range(len(input_messages)):input_messages[i].group_id = self.group.id@@ -70,7 +75,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):billing_context=billing_context,)- run_ids = await self.run_sleeptime_agents()+ run_ids = await self.run_sleeptime_agents(billing_context=billing_context)response.usage.run_ids = run_idsreturn response@@ -92,6 +97,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):openai_responses_websocket: bool = False,) -> AsyncGenerator[str, None]:self.run_ids = []+ self._billing_context = billing_contextfor i in range(len(input_messages)):input_messages[i].group_id = self.group.id@@ -116,13 +122,15 @@ class SleeptimeMultiAgentV4(LettaAgentV3):finally:# For some reason, stream is throwing a GeneratorExit even though it appears the that client# is getting the whole stream. This pattern should work to ensure sleeptime agents run despite this.- await self.run_sleeptime_agents()+ await self.run_sleeptime_agents(billing_context=billing_context)@trace_method- async def run_sleeptime_agents(self) -> list[str]:+ async def run_sleeptime_agents(self, billing_context: BillingContext | None = None) -> list[str]:# Get response messageslast_response_messages = self.response_messages+ billing_context = billing_context or self._billing_context+# Update turns counterturns_counter = Noneif self.group.sleeptime_agent_frequency is not None and self.group.sleeptime_agent_frequency > 0:@@ -146,6 +154,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):sleeptime_agent_id,last_response_messages,last_processed_message_id,+ billing_context,)self.run_ids.append(sleeptime_run_id)except Exception as e:@@ -160,6 +169,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):sleeptime_agent_id: str,response_messages: list[Message],last_processed_message_id: str,+ billing_context: BillingContext | None,) -> str:run = Run(agent_id=sleeptime_agent_id,@@ -178,6 +188,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):response_messages=response_messages,last_processed_message_id=last_processed_message_id,run_id=run.id,+ billing_context=billing_context,),label=f"participant_agent_step_{sleeptime_agent_id}",)@@ -191,6 +202,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):response_messages: list[Message],last_processed_message_id: str,run_id: str,+ billing_context: BillingContext | None,) -> LettaResponse:try:# Update run status@@ -247,6 +259,7 @@ class SleeptimeMultiAgentV4(LettaAgentV3):result = await sleeptime_agent.step(input_messages=sleeptime_agent_messages,run_id=run_id,+ billing_context=billing_context,)# Update run statusdiff --git a/letta/server/rest_api/proxy_helpers.py b/letta/server/rest_api/proxy_helpers.pyindex 644132b5e..d7eec5aba 100644--- a/letta/server/rest_api/proxy_helpers.py+++ b/letta/server/rest_api/proxy_helpers.py@@ -338,6 +338,7 @@ async def persist_messages_background(assistant_message: str,model_name: str,proxy_name: str,+ billing_context=None,):"""Background task to persist messages without blocking the response.@@ -361,6 +362,7 @@ async def persist_messages_background(user_messages=user_messages,assistant_message=assistant_message,model=model_name,+ billing_context=billing_context,)if result.get("success"):logger.info(f"[{proxy_name}] Persisted messages: {result['messages_created']} messages saved")diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 88b0325f0..d0440805f 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -2536,6 +2536,6 @@ async def capture_messages(if sleeptime_group:sleeptime_agent_loop = SleeptimeMultiAgentV4(agent_state=agent, actor=actor, group=sleeptime_group)sleeptime_agent_loop.response_messages = response_messages- run_ids = await sleeptime_agent_loop.run_sleeptime_agents()+ run_ids = await sleeptime_agent_loop.run_sleeptime_agents(billing_context=headers.billing_context)return JSONResponse({"success": True, "messages_created": len(response_messages), "run_ids": run_ids})diff --git a/letta/server/rest_api/utils.py b/letta/server/rest_api/utils.pyindex 8d2fff69e..fd4ebf35c 100644--- a/letta/server/rest_api/utils.py+++ b/letta/server/rest_api/utils.py@@ -37,6 +37,7 @@ from letta.schemas.letta_message_content import ()from letta.schemas.llm_config import LLMConfigfrom letta.schemas.message import ApprovalCreate, Message, MessageCreate, ToolReturn+from letta.schemas.provider_trace import BillingContextfrom letta.schemas.tool_execution_result import ToolExecutionResultfrom letta.schemas.usage import LettaUsageStatisticsfrom letta.schemas.user import User@@ -801,6 +802,7 @@ async def capture_and_persist_messages(user_messages: list[str],assistant_message: str,model: Optional[str] = None,+ billing_context: BillingContext | None = None,) -> Dict[str, Any]:"""Capture user and assistant messages and persist them to the database.@@ -862,7 +864,7 @@ async def capture_and_persist_messages(sleeptime_agent_loop = SleeptimeMultiAgentV4(agent_state=agent, actor=actor, group=sleeptime_group)sleeptime_agent_loop.response_messages = response_messages- run_ids = await sleeptime_agent_loop.run_sleeptime_agents()+ run_ids = await sleeptime_agent_loop.run_sleeptime_agents(billing_context=billing_context)logger.info(f"Triggered sleeptime agents, run_ids: {run_ids}")except Exception as e: