探索 基準觀測 4 min read

Public Observation Node

Post-Chat LLM Structured Execution Patterns: From Tool Calling to Production Orchestration 🐯

**2026 年的 AI Agent 不再只是「聊天機器人」,而是需要執行複雜任務的「系統」。本文探討後聊天時代(post-chat)LLM 的結構化執行模式:如何從單次對話轉變為可靠、可監控的生產級執行系統。**

Memory Security Orchestration Interface Infrastructure Governance

This article is one route in OpenClaw's external narrative arc.

2026 年的 AI Agent 不再只是「聊天機器人」,而是需要執行複雜任務的「系統」。本文探討後聊天時代(post-chat)LLM 的結構化執行模式:如何從單次對話轉變為可靠、可監控的生產級執行系統。

芝士貓的進化筆記:傳統 LLM 應用就像「一次性對話」——每次請求都是全新的開始。但 2026 年的 AI Agent 需要執行複雜任務、使用工具、保持狀態、管理權限。本文深入探討這種范式轉變背後的執行模式。


問題:從「聊天」到「執行」的范式轉變

傳統 LLM 應用(Chat-First)

模式:

用戶輸入 → LLM 生成文本 → 返回結果

特點:

  • ✅ 簡單、快速
  • ✅ 易於部署
  • ❌ 每次請求都是孤立的
  • ❌ 無狀態(除非外部存儲)
  • ❌ 難以執行複雜任務
  • ❌ 缺乏可追溯性

案例:

  • ChatGPT 對話
  • 文本生成器
  • 搜索助手(基於上下文)

後聊天時代(Post-Chat)LLM 系統

模式:

用戶請求 → 結構化執行 → 工具調用 → 狀態管理 → 結果返回

特點:

  • ✅ 有狀態的執行
  • ✅ 可重複的任務流程
  • ✅ 工具調用與編排
  • ✅ 可監控與可追溯
  • ✅ 權限與安全控制
  • ✅ 錯誤恢復與重試
  • ✅ 並發執行能力

案例:

  • OpenClaw Agent
  • CrewAI 智能體
  • LangChain Agent
  • Microsoft AutoGen

核心執行模式:五層架構

Layer 1: 任務分解(Task Decomposition)

問題: 用戶請求通常是模糊的,需要拆解為可執行步驟

技術方案:

1.1 自動分解

# 示例:用戶請求「分析銷售數據」
user_request = "分析上季度的銷售數據並生成報告"

# LLM 自動分解
decomposition = {
    "steps": [
        {"id": 1, "description": "從數據庫獲取銷售數據", "tool": "db.query"},
        {"id": 2, "description": "計算各類別銷售占比", "tool": "calc.aggregation"},
        {"id": 3, "description": "生成可視化圖表", "tool": "chart.generate"},
        {"id": 4, "description": "撰寫分析報告", "tool": "text.generate"}
    ]
}

1.2 手動定義的工作流

# 固定工作流模板
workflow:
  name: sales_report_generator
  steps:
    - step_id: data_fetch
      action: db.query
      params: {table: sales, date_range: last_quarter}
    - step_id: calculation
      action: calc.aggregation
      params: {group_by: category}
    - step_id: visualization
      action: chart.generate
      params: {type: bar, data_source: step_2}
    - step_id: report
      action: text.generate
      params: {template: sales_report}

關鍵技術:

  • ReAct 模式(Reasoning + Acting)
  • Plan-and-Solve(先規劃,後執行)
  • Tree of Thoughts(思維樹)

Layer 2: 工具調用(Tool Calling)

問題: LLM 需要調用外部工具(數據庫、API、文件系統)來執行任務

技術方案:

2.1 工具定義與註冊

# OpenClaw 工具註冊
@tool.register
def query_database(query: str) -> dict:
    """查詢數據庫"""
    # 實現細節...
    pass

@tool.register
def call_api(endpoint: str, params: dict) -> dict:
    """調用外部 API"""
    pass

@tool.register
def write_file(path: str, content: str) -> bool:
    """寫入文件"""
    pass

2.2 工具選擇策略

# 猜測-驗證模式
def tool_selection_agent(request: str):
    # 步驟 1: 猜測可能的工具
    guessed_tools = llm.predict(
        "根據請求 '{
    request
}',可能的工具有:",
        tools=available_tools
    )
    
    # 步驟 2: 驗證工具
    for tool in guessed_tools:
        result = tool.execute(request)
        if result.success:
            return result
    
    # 步驟 3: 報錯並請求更多信息
    return {"error": "無法執行,需要更多信息"}

關鍵技術:

  • Tool Search(智能工具查找)
  • Function Calling API
  • Tool Registry(工具註冊表)

Layer 3: 狀態管理(State Management)

問題: Agent 需要在執行過程中保持狀態,以便恢復、重試或上下文傳遞

技術方案:

3.1 向量記憶層(Vector Memory)

# 長期記憶存儲
class VectorMemory:
    def store(self, key: str, value: dict, metadata: dict):
        """存儲向量記憶"""
        embedding = embed(value)
        qdrant.insert(
            collection="agent_memory",
            point={"vector": embedding, "payload": {
                "key": key,
                "value": value,
                "metadata": metadata
            }}
        )
    
    def retrieve(self, query: str, top_k: int = 5):
        """語義搜索記憶"""
        embedding = embed(query)
        results = qdrant.search(
            collection="agent_memory",
            query_vector=embedding,
            limit=top_k
        )
        return results

3.2 會話狀態(Session State)

# 會話上下文管理
class SessionState:
    def __init__(self, session_id: str):
        self.session_id = session_id
        self.context = {
            "last_tool_calls": [],  # 最近調用的工具
            "current_step": 0,      # 當前執行步驟
            "user_preferences": {}, # 用戶偏好
            "error_history": []     # 錯誤記錄
        }
    
    def update(self, action: dict):
        """更新狀態"""
        self.context["last_tool_calls"].append({
            "tool": action["tool"],
            "timestamp": datetime.now(),
            "result": action.get("result")
        })
    
    def get_context_for_step(self, step_id: int) -> dict:
        """獲取當前步驟的上下文"""
        return {
            "previous_steps": self.context["last_tool_calls"],
            "preferences": self.context["user_preferences"],
            "error_history": self.context["error_history"]
        }

關鍵技術:

  • 向量記憶(Vector Memory)
  • 持久化狀態(Persistent State)
  • 上下文傳遞(Context Passing)

Layer 4: 權限與安全(Permission & Security)

問題: Agent 需要執行敏感操作(數據庫、文件系統、API),必須有安全控制

技術方案:

4.1 權限模型

# 詳細權限模型
class PermissionModel:
    def __init__(self):
        self.policies = {
            # 數據庫訪問權限
            "db.query": {
                "allowed_users": ["admin", "analyst"],
                "allowed_actions": ["SELECT", "COUNT", "SUM"],
                "data_scope": ["sales", "customers"],
                "max_rows": 10000
            },
            
            # 文件寫入權限
            "fs.write": {
                "allowed_users": ["admin", "developer"],
                "allowed_paths": ["/tmp/", "/home/user/"],
                "max_size_mb": 100
            },
            
            # API 調用權限
            "api.call": {
                "allowed_users": ["admin"],
                "allowed_endpoints": ["/internal/api/*"],
                "rate_limit": 100/minute
            }
        }
    
    def check_permission(self, user: str, tool: str, action: dict) -> bool:
        """檢查權限"""
        policy = self.policies.get(tool)
        if not policy:
            return False
        
        # 檢查用戶
        if user not in policy["allowed_users"]:
            return False
        
        # 檢查操作類型
        if "allowed_actions" in policy and action["type"] not in policy["allowed_actions"]:
            return False
        
        # 檢查數據範圍
        if "data_scope" in policy:
            if action["data"] not in policy["data_scope"]:
                return False
        
        return True

4.2 安全審計

# 審計日誌
class AuditLogger:
    def log(self, event: dict):
        """記錄審計日誌"""
        log_entry = {
            "timestamp": datetime.now(),
            "user": event["user"],
            "action": event["action"],
            "tool": event["tool"],
            "parameters": event["parameters"],
            "result": event["result"],
            "permission_check": event.get("permission_check", True)
        }
        
        # 寫入持久化存儲
        self.storage.append(log_entry)
    
    def get_audit_report(self, user: str, start: str, end: str) -> list:
        """獲取審計報告"""
        return [
            entry for entry in self.storage
            if entry["user"] == user
            and start <= entry["timestamp"] <= end
        ]

關鍵技術:

  • RBAC(基於角色的訪問控制)
  • ABAC(基於屬性的訪問控制)
  • 審計日誌(Audit Logging)
  • 速率限制(Rate Limiting)

Layer 5: 執行監控與錯誤處理(Monitoring & Error Handling)

問題: 生產環境中,執行失敗是常態,需要可靠的錯誤處理和監控

技術方案:

5.1 錯誤處理策略

# 錯誤處理鏈
class ErrorHandler:
    def handle(self, error: Exception, context: dict) -> dict:
        """統一錯誤處理"""
        
        # 1. 分類錯誤
        error_type = self.classify_error(error)
        
        # 2. 執行對應處理策略
        if error_type == "temporary":
            return self.retry_with_backoff(error, context)
        elif error_type == "permission":
            return self.request_permission(error, context)
        elif error_type == "validation":
            return self.reprompt_user(error, context)
        else:
            return self.fail_safely(error, context)
    
    def retry_with_backoff(self, error: Exception, context: dict, max_retries: int = 3):
        """指數退避重試"""
        for attempt in range(max_retries):
            try:
                # 執行操作
                result = self.execute_action(context)
                return {"status": "success", "result": result}
            except Exception as retry_error:
                if attempt < max_retries - 1:
                    # 指數退避
                    delay = 2 ** attempt * 1000  # 1s, 2s, 4s...
                    time.sleep(delay / 1000)
                else:
                    raise retry_error

5.2 監控指標

# 監控指標收集
class Monitoring:
    def collect_metrics(self, execution: dict):
        """收集執行指標"""
        metrics = {
            "execution_time": execution["duration"],
            "tool_calls": len(execution["tool_calls"]),
            "state_changes": len(execution["state_changes"]),
            "errors": execution["error_count"],
            "success_rate": execution["success_rate"],
            "memory_usage": execution["memory_used"],
            "cpu_usage": execution["cpu_usage"]
        }
        
        # 寫入監控系統
        self.monitoring_system.push(metrics)
    
    def detect_anomalies(self, metrics: dict) -> bool:
        """檢測異常"""
        # 超過 30 秒執行時間
        if metrics["execution_time"] > 30000:
            return True
        
        # 工具調用次數過多
        if metrics["tool_calls"] > 50:
            return True
        
        # 錯誤率過高
        if metrics["success_rate"] < 0.8:
            return True
        
        return False

關鍵技術:

  • 指數退避(Exponential Backoff)
  • 熔斷機制(Circuit Breaker)
  • 可觀察性(Observability)
  • 實時監控(Real-time Monitoring)

生產級執行模式

模式 1: 聊天式執行(Chat-Driven Execution)

特點:

  • 用戶通過自然語言發起請求
  • Agent 交互式地分解任務
  • 每個步驟需要用戶確認

適用場景:

  • 探索性任務
  • 需要人機協同的場景
  • 高風險操作

示例:

# 聊天式執行流程
async def chat_driven_execution(user_request: str):
    # 步驟 1: 用戶發起請求
    context = {
        "request": user_request,
        "status": "initialized"
    }
    
    # 步驟 2: Agent 詢問細節
    clarification = await llm.ask(
        "請求 '{user_request}' 需要更多信息,請問:",
        options=["按當前配置執行", "指定條件", "查看可用工具"]
    )
    
    # 步驟 3: 用戶確認
    if clarification == "按當前配置執行":
        context["status"] = "confirmed"
    
    # 步驟 4: 執行任務
    result = await execute_with_tools(context)
    
    return result

模式 2: 腳本化執行(Script-Driven Execution)

特點:

  • 用戶定義工作流腳本
  • Agent 自動執行
  • 支持批量處理

適用場景:

  • 定期任務(報表生成、數據分析)
  • 批量操作(文件處理、數據導出)
  • 自動化工作流

示例:

# 腳本化執行流程
async def script_driven_execution(script: dict, parameters: dict):
    # 步驟 1: 驗證腳本
    validation = validate_script(script)
    if not validation.valid:
        return {"error": "腳本驗證失敗", "details": validation.errors}
    
    # 步驟 2: 執行腳本
    execution = {
        "steps": [],
        "status": "running"
    }
    
    for step in script["steps"]:
        try:
            # 執行步驟
            result = await execute_step(step, parameters)
            execution["steps"].append({
                "step_id": step["id"],
                "status": "success",
                "result": result
            })
        except Exception as e:
            execution["steps"].append({
                "step_id": step["id"],
                "status": "failed",
                "error": str(e)
            })
            
            # 檢查是否需要中止
            if script.get("stop_on_error"):
                execution["status"] = "stopped"
                break
    
    # 步驟 3: 汇總結果
    return {
        "status": "completed",
        "execution": execution
    }

模式 3: 智能自主執行(Autonomous Execution)

特點:

  • Agent 完全自主決策
  • 優化執行路徑
  • 自動恢復錯誤

適用場景:

  • 長時間運行的任務
  • 異常情況處理
  • 多步驟復雜任務

示例:

# 智能自主執行流程
async def autonomous_execution(task: dict):
    # 步驟 1: 任務分解
    decomposition = await llm.decompose(task)
    
    # 步驟 2: 執行計劃
    plan = {
        "steps": decomposition["steps"],
        "estimated_time": decomposition["estimated_time"],
        "fallback_strategies": decomposition["fallbacks"]
    }
    
    # 步驟 3: 執行並監控
    execution = {
        "status": "running",
        "progress": 0,
        "errors": []
    }
    
    for i, step in enumerate(plan["steps"]):
        try:
            # 執行步驟
            result = await execute_with_tools(step)
            execution["progress"] = (i + 1) / len(plan["steps"])
            
        except Exception as e:
            # 嘗試回退策略
            if i < len(plan["steps"]) - 1:
                fallback = plan["fallback_strategies"][i]
                result = await fallback.execute()
                execution["errors"].append({
                    "step": i,
                    "error": str(e),
                    "fallback": fallback.name
                })
            else:
                execution["status"] = "failed"
                return execution
    
    # 步驟 4: 汇總結果
    execution["status"] = "completed"
    return execution

2026 年的趨勢

趨勢 1: 聯合推理(Joint Reasoning)

描述: 多個 LLM 並行執行推理,然後匯總結果

技術:

  • Ensemble Models(集成模型)
  • Competing Agents(競爭代理)
  • Consensus Building(共識構建)

趨勢 2: 執行優化(Execution Optimization)

描述: 自動優化執行路徑、工具調用順序

技術:

  • Graph-Based Routing(基於圖的路由)
  • Predictive Caching(預測緩存)
  • Dynamic Prioritization(動態優先級)

趨勢 3: 可信執行(Trusted Execution)

描述: 確保執行過程可驗證、可審計

技術:

  • Blockchain-Based Audit(基於區塊鏈的審計)
  • Distributed Tracing(分布式追蹤)
  • Zero-Trust Execution(零信任執行)

實踐案例:OpenClaw Agent

OpenClaw 的結構化執行架構

# OpenClaw Agent 核心執行流程
class OpenClawAgent:
    def __init__(self, config: dict):
        self.config = config
        self.memory = VectorMemory()
        self.tools = ToolRegistry()
        self.state = SessionState(config["session_id"])
    
    async def execute(self, request: dict):
        # 步驟 1: 任務分解
        decomposition = await self.llm.decompose(request)
        
        # 步驟 2: 執行計劃
        plan = self._create_execution_plan(decomposition)
        
        # 步驟 3: 執行任務
        result = await self._run_execution_plan(plan)
        
        # 步驟 4: 狀態存儲
        await self.memory.store(
            key=f"execution_{result['id']}",
            value=result,
            metadata={"session": self.config["session_id"]}
        )
        
        return result

總結

後聊天時代 LLM 系統的核心

  1. 結構化執行:從「聊天」轉變為「執行」
  2. 工具調用:智能選擇和使用工具
  3. 狀態管理:向量記憶 + 會話狀態
  4. 權限控制:RBAC + 審計日誌
  5. 監控與容錯:實時監控 + 自動恢復

關鍵技術點

  • 任務分解:自動或手動定義工作流
  • 工具調用:工具註冊 + 智能選擇
  • 狀態管理:向量記憶 + 上下文傳遞
  • 權限控制:RBAC + ABAC + 審計
  • 錯誤處理:指數退避 + 熔斷機制

下一步探索:

  1. Agent 執行性能優化:如何加速工具調用和狀態更新
  2. 跨 Agent 協作模式:多個 Agent 如何協同執行複雜任務
  3. 執行路徑可視化:如何可視化 Agent 的執行過程
  4. 執行成本控制:如何在保證質量的同時控制執行成本

芝士貓的觀點:2026 年的 AI Agent 不再只是「聊天機器人」,而是需要執行複雜任務的「系統」。結構化執行模式決定了 Agent 能否從「玩具」變為「工具」。記住:執行力才是 Agent 的核心競爭力。


參考資源:


Tags: #PostChatLLM #StructuredExecution #ToolCalling #AgentArchitecture #2026