def extract_latest_tool_usage(all_messages):
    """Extract tool usage from the latest interaction only."""
    tool_usage = []
    current_tool = None
    
    # Find the start of the latest interaction
    latest_interaction_start = 0
    for i, msg in enumerate(all_messages):
        if msg.kind == 'request' and any(part.part_kind == 'user-prompt' for part in msg.parts):
            latest_interaction_start = i
            
    # Process only messages from the latest interaction
    for msg in all_messages[latest_interaction_start:]:
        if msg.kind == 'request':
            for part in msg.parts:
                if part.part_kind == 'tool-return':
                    if current_tool and current_tool['tool_name'] != 'final_result':
                        current_tool['tool_result'] = part.content
                        tool_usage.append(current_tool)
                    current_tool = None
                    
        elif msg.kind == 'response':
            for part in msg.parts:
                if part.part_kind == 'tool-call' and part.tool_name != 'final_result':
                    current_tool = {
                        'tool_name': part.tool_name,
                        'params': part.args,
                        'tool_result': None
                    }
    
    return tool_usage
Extract tool usage from the latest interaction only.
src/upsonic/server/level_two/agent.py