@@ -273,6 +273,9 @@ async def create_message(self, message: NewMessage) -> Message:
273273"model" :message .resource_usage .model ,
274274"total_input_tokens" :message .resource_usage .total_input_tokens ,
275275"total_output_tokens" :message .resource_usage .total_output_tokens ,# noqa: E501
276+ "user_token" :message .resource_usage .user_token ,
277+ "time_to_first_token" :message .resource_usage .time_to_first_token ,# noqa: E501
278+ "tokens_per_second" :message .resource_usage .tokens_per_second ,
276279"total_run_time" :message .resource_usage .total_run_time ,
277280 },
278281 )
@@ -515,3 +518,50 @@ async def get_next_ai_message(
515518toolCalls = message .tool_calls or [],
516519resource_usage = resource_usage ,
517520 )
521+
522+ async def update_message_resource_usage (
523+ self ,
524+ message_id :str ,
525+ resource_usage :ResourceUsage ,
526+ )-> None :
527+ """Update or create resource usage for a message.
528+
529+ Args:
530+ message_id: Unique identifier for the message.
531+ resource_usage: ResourceUsage data to update or create.
532+ """
533+ # Check if resource usage already exists for this message
534+ query = select (ORMResourceUsage ).where (
535+ ORMResourceUsage .message_id == message_id
536+ )
537+ existing = await self ._session .scalar (query )
538+
539+ if existing :
540+ # Update existing resource usage
541+ update_query = (
542+ update (ORMResourceUsage )
543+ .where (ORMResourceUsage .message_id == message_id )
544+ .values (
545+ model = resource_usage .model ,
546+ total_input_tokens = resource_usage .total_input_tokens ,
547+ total_output_tokens = resource_usage .total_output_tokens ,
548+ user_token = resource_usage .user_token ,
549+ time_to_first_token = resource_usage .time_to_first_token ,
550+ tokens_per_second = resource_usage .tokens_per_second ,
551+ total_run_time = resource_usage .total_run_time ,
552+ )
553+ )
554+ await self ._session .execute (update_query )
555+ else :
556+ # Create new resource usage
557+ insert_query = insert (ORMResourceUsage ).values (
558+ message_id = message_id ,
559+ model = resource_usage .model ,
560+ total_input_tokens = resource_usage .total_input_tokens ,
561+ total_output_tokens = resource_usage .total_output_tokens ,
562+ user_token = resource_usage .user_token ,
563+ time_to_first_token = resource_usage .time_to_first_token ,
564+ tokens_per_second = resource_usage .tokens_per_second ,
565+ total_run_time = resource_usage .total_run_time ,
566+ )
567+ await self ._session .execute (insert_query )