Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix(core): fix billing and auto credit top-up for letta/auto mode (#10073)

cthomas9fc86eced7e5
diff --git a/letta/agents/letta_agent_v3.py b/letta/agents/letta_agent_v3.py
index f8f5d59cf..1e85ba076 100644
--- a/letta/agents/letta_agent_v3.py
+++ b/letta/agents/letta_agent_v3.py
@@ -1052,6 +1052,18 @@ class LettaAgentV3(LettaAgentV2):
# Update the adapter to use the resolved client and config
llm_adapter.llm_client = active_llm_client
llm_adapter.llm_config = active_llm_config
+ # Update persisted step with resolved model info so billing can
+ # identify the actual model and charge at the correct rate.
+ # Keep model_handle as the original auto handle so we can still
+ # identify that the user was on auto mode.
+ await self.step_manager.update_step_resolved_model_async(
+ actor=self.actor,
+ step_id=step_id,
+ provider_name=active_llm_config.model_endpoint_type,
+ provider_category=active_llm_config.provider_category or "base",
+ model=active_llm_config.model,
+ model_endpoint=active_llm_config.model_endpoint,
+ )
else:
active_llm_config = self.agent_state.llm_config
active_llm_client = self.llm_client
diff --git a/letta/services/step_manager.py b/letta/services/step_manager.py
index 019ff403f..ff0348d11 100644
--- a/letta/services/step_manager.py
+++ b/letta/services/step_manager.py
@@ -474,6 +474,44 @@ class StepManager:
await webhook_service.notify_step_complete(step_id)
return pydantic_step
+ @enforce_types
+ @raise_on_invalid_id(param_name="step_id", expected_prefix=PrimitiveType.STEP)
+ @trace_method
+ async def update_step_resolved_model_async(
+ self,
+ actor: PydanticUser,
+ step_id: str,
+ provider_name: str,
+ provider_category: str,
+ model: str,
+ model_endpoint: Optional[str],
+ model_handle: Optional[str] = None,
+ ) -> None:
+ """Update a step with resolved model info (e.g. after auto mode resolution).
+
+ Args:
+ actor: The user making the request
+ step_id: The ID of the step to update
+ provider_name: The resolved provider name
+ provider_category: The resolved provider category
+ model: The resolved model name
+ model_endpoint: The resolved model endpoint
+ model_handle: The resolved model handle
+ """
+ async with db_registry.async_session() as session:
+ step = await session.get(StepModel, step_id)
+ if not step:
+ raise NoResultFound(f"Step with id {step_id} does not exist")
+ if step.organization_id != actor.organization_id:
+ raise Exception("Unauthorized")
+
+ step.provider_name = provider_name
+ step.provider_category = provider_category
+ step.model = model
+ step.model_endpoint = model_endpoint
+ if model_handle is not None:
+ step.model_handle = model_handle
+
@enforce_types
@raise_on_invalid_id(param_name="step_id", expected_prefix=PrimitiveType.STEP)
@trace_method
@@ -754,6 +792,20 @@ class NoopStepManager(StepManager):
) -> PydanticStep:
return
+ @enforce_types
+ @trace_method
+ async def update_step_resolved_model_async(
+ self,
+ actor: PydanticUser,
+ step_id: str,
+ provider_name: str,
+ provider_category: str,
+ model: str,
+ model_endpoint: Optional[str],
+ model_handle: Optional[str] = None,
+ ) -> None:
+ return
+
@enforce_types
@trace_method
async def update_step_cancelled_async(