解析生产级AI客服agent核心架构,区分intent分类、tool执行、response生成三阶段,提供完整Python异步循环代码示例,重点讲解tool-use层的工程实现细节。
大多数客服 AI 实现只回答问题。它们从知识库中检索相关信息,综合出一个回复,然后把对话交还客户。
这叫聊天机器人。2026 年的好聊天机器人,但仍然是聊天机器人。
一个能执行工作流的客服 Agent 做的不同。它处理退款。它更新账户。它触发退货标签。它在相关系统中完成客户要求的事情,并在完成后发送确认。区别不在于模型,而在于模型周围的架构,特别是工具调用层以及连接它的所有部分是如何设计的。
这是我们在生产环境中使用的客服 Agent 架构。这是大多数教程跳过的部分。
问答 Agent 只有一个主要操作:检索上下文、生成回复。循环很简单。
工作流执行 Agent 有三个:分类意图、执行工具、生成回复。中间步骤是生产级复杂度的所在。
下面是管理一切的 Agent 循环:
from anthropic import AsyncAnthropic
from typing import Optional
import asyncio
client = AsyncAnthropic()
async def agent_loop(conversation: Conversation) -> AgentResponse:
# Step 1: Classify customer intent
intent = await classify_intent(
message=conversation.latest_message,
history=conversation.history
)
# Step 2: Retrieve customer context from backend systems
context = await retrieve_context(
customer_id=conversation.customer_id,
intent=intent
)
# Step 3: Plan actions based on intent + context
action_plan = await plan_actions(
intent=intent,
context=context,
policy=load_policy(intent.type)
)
# Step 4: Execute tools if the intent requires action
tool_results = {}
if action_plan.requires_tools:
tool_results = await execute_tools(action_plan.tools)
# Check escalation conditions before proceeding
if should_escalate(tool_results, intent, context):
return await escalate_to_human(
conversation=conversation,
context=context,
tool_results=tool_results,
reason=determine_escalation_reason(intent, tool_results)
)
# Step 5: Generate grounded response from results
response = await generate_response(
intent=intent,
context=context,
tool_results=tool_results
)
# Step 6: Persist updated conversation state
await persist_context(conversation, response, tool_results)
return response
与纯检索 Agent 的关键区别:第 4 步对真实系统执行真实操作。Agent 不是在描述应该发生什么,而是在使其发生。
在任何工具调用发生之前,Agent 需要知道它处理的是哪类请求。Intent 分类决定哪些工具会被考虑,以及哪些策略规则会适用。
INTENT_CATEGORIES = [
"order_status",
"return_request",
"refund_request",
"account_update",
"billing_dispute",
"product_question",
"complaint",
"explicit_escalation"
]
async def classify_intent(message: str, history: list) -> Intent:
response = await client.messages.create(
model="claude-sonnet-4-5",
max_tokens=256,
system="""Classify the customer message into exactly one intent category.
Return JSON: {"type": <category>, "confidence": <0-1>, "entities": {}}
Categories: order_status, return_request, refund_request, account_update,
billing_dispute, product_question, complaint, explicit_escalation""",
messages=[
{"role": "user", "content": f"History: {history[-3:]}\nMessage: {message}"}
]
)
return Intent.from_json(response.content[0].text)
这里的置信度评分不只用于路由。当 intent 分类置信度低于 0.70 时,这是收紧工具权限范围和降低升级阈值的信号——Agent 对客户实际需求的不确定性更大。
这正是架构与纯检索设计分叉的地方。工具是 Agent 与后端系统、CRM、订单管理、支付处理、客服台之间的接口。
CUSTOMER_SERVICE_TOOLS = [
{
"name": "lookup_order",
"description": "Retrieve current order status, tracking, and line items for a customer order",
"input_schema": {
"type": "object",
"properties": {
"order_id": {"type": "string"},
"customer_id": {"type": "string"}
},
"required": ["order_id", "customer_id"]
}
},
{
"name": "process_refund",
"description": "Initiate a refund for an eligible order within policy parameters",
"input_schema": {
"type": "object",
"properties": {
"order_id": {"type": "string"},
"refund_amount": {"type": "number"},
"reason": {"type": "string"},
"policy_check_passed": {"type": "boolean"}
},
"required": ["order_id", "refund_amount", "reason", "policy_check_passed"]
}
},
{
"name": "create_return_label",
"description": "Generate a prepaid return shipping label and initiate return workflow",
"input_schema": {
"type": "object",
"properties": {
"order_id": {"type": "string"},
"return_reason": {"type": "string"}
},
"required": ["order_id", "return_reason"]
}
},
{
"name": "update_account_field",
"description": "Update a customer account detail after identity verification",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {"type": "string"},
"field": {"type": "string", "enum": ["email", "address", "phone"]},
"new_value": {"type": "string"},
"verified": {"type": "boolean"}
},
"required": ["customer_id", "field", "new_value", "verified"]
}
}
]
注意 policy_check_passed 和 verified 在退款和账户更新工具中是必填字段。Agent 不能不明确确认策略资格已检查、身份已验证就调用这些工具。这是在工具签名级别强制执行,而不是在 prompt 级别——这是更难绕过的约束。
一个在网页聊天开始对话、通过邮件跟进、然后致电语音客服的客户,不应该在每次交接时重新解释情况。上下文持久化是多渠道支持感觉像一个对话而不是三个独立对话的原因。
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any
@dataclass
class ConversationContext:
customer_id: str
conversation_id: str
channel: str # "web_chat" | "email" | "voice" | "whatsapp"
# Verified information (survives channel switches)
verified_identity: bool = False
verified_order_id: Optional[str] = None
verified_fields: Dict[str, Any] = field(default_factory=dict)
# Resolution tracking
attempted_resolutions: List[Dict] = field(default_factory=list)
current_intent: Optional[str] = None
# Escalation state
escalation_reason: Optional[str] = None
escalation_priority: str = "standard" # "standard" | "high" | "critical"
# Cross-channel history
prior_conversations: List[str] = field(default_factory=list)
unresolved_issues: List[str] = field(default_factory=list)
async def load_or_create_context(customer_id: str, channel: str) -> ConversationContext:
# Check for existing unresolved context across channels
existing = await redis_client.get(f"context:{customer_id}:active")
if existing:
context = ConversationContext.from_json(existing)
context.channel = channel # Update current channel
return context
# Load customer history to pre-populate context
customer = await crm.get_customer(customer_id)
return ConversationContext(
customer_id=customer_id,
conversation_id=generate_id(),
channel=channel,
prior_conversations=customer.recent_conversation_ids,
unresolved_issues=customer.open_tickets
)
verified_fields 字典特别重要。当客户在网页聊天中验证了身份,该验证会持久化到语音会话中。新渠道的 Agent 知道哪些已经确认,不会要求客户重新验证。
Escalation 逻辑是生产级质量与 demo 质量区分的地方。错误升级的 Agent 会让客户沮丧。该升级而不升级的 Agent 会造成责任风险。
LEGAL_KEYWORDS = [
"attorney", "lawyer", "lawsuit", "legal action",
"sue", "court", "fraud", "chargeback dispute"
]
SECURITY_KEYWORDS = [
"hacked", "unauthorized", "data breach",
"identity theft", "fraud", "compromised"
]
def should_escalate(
tool_results: Dict,
intent: Intent,
context: ConversationContext
) -> bool:
# Hard rules, always escalate regardless of confidence
if intent.type == "explicit_escalation":
return True
if contains_any(context.latest_message, LEGAL_KEYWORDS):
return True
if contains_any(context.latest_message, SECURITY_KEYWORDS):
return True
# VIP / high-value customer handling
if context.customer_tier == "enterprise":
if len(context.attempted_resolutions) >= 2:
return True
# Confidence-based escalation
if intent.confidence < 0.65:
return True
if tool_results.get("resolution_confidence", 1.0) < 0.70:
return True
# Time and turn limits
if context.turn_count > 8:
return True
if context.elapsed_seconds > 600:
return True
return False
企业客户分层升级阈值(>= 2 次尝试 vs 标准 > 8 轮)反映了一项业务决策:高价值客户获得更快的人工接入权限。那条策略写在代码里,而不是 prompt 里,这意味着它是可强制执行和可审计的。
Handoff payload 是大多数 Agent 架构中构建最薄弱的部分。如果接手升级的人类 Agent 必须重新阅读记录并从头重建上下文,那么升级体验比客户一开始就直接打电话给人工还差。
async def escalate_to_human(
conversation: Conversation,
context: ConversationContext,
tool_results: Dict,
reason: str
) -> AgentResponse:
# Generate AI summary of conversation for the human agent
summary = await generate_handoff_summary(conversation, context, tool_results)
handoff_payload = {
"conversation_id": conversation.id,
"customer": {
"id": context.customer_id,
"name": context.customer_name,
"tier": context.customer_tier,
"lifetime_value": context.customer_ltv,
"sentiment": context.sentiment_score
},
"summary": summary,
"escalation_reason": reason,
"escalation_priority": context.escalation_priority,
"attempted_resolutions": context.attempted_resolutions,
"verified_context": context.verified_fields,
"recommended_action": await suggest_next_action(context, tool_results),
"full_transcript": conversation.messages,
"open_tickets": context.unresolved_issues
}
# Route to appropriate queue based on priority and type
queue = determine_queue(reason, context.customer_tier)
ticket_id = await helpdesk.create_escalation(queue, handoff_payload)
# Inform the customer honest about what's happening
return AgentResponse(
message=f"I'm connecting you with a specialist who can help with this. "
f"They'll have the full context of our conversation "
f"you won't need to repeat anything. Reference: {ticket_id}",
action="escalate",
ticket_id=ticket_id
)
Payload 中的 recommended_action 字段是显著改变平均处理时间的部分。当人工 Agent 打开案件时,他们得到的是结构化建议,而不是需要重建的记录。AI 没有转移问题,它转移的是一个可决策的包。
正确构建这个架构能把 Agent 从问答提升到工作流执行。它解决不了的是工具下面的集成层——让 process_refund() 可靠地连接到你的实际支付处理器、你的实际权限模型、处理你的实际错误状态。
上面的工具签名是有意简化干净的。背后的实现才是生产级复杂度的所在:限流、重试逻辑、当下游 API 在对话中途失败时的熔断、每次写操作的审计日志。
在工具定义和工具在生产负载下正常工作之间的差距,是大多数企业级 Agent 项目要么正确投入、要么发现本应如此的分水岭。
聊天是简单的部分。工作流执行和后端集成才是生产 Agent 成败之所。我们写了完整的构建指南,涵盖完整的八层架构、可靠性模式、成本建模和在规模下摧毁 Agent 的失败模式。
How to Build a 24/7 AI Customer Service Agent, Enterprise Guide
Dextra Labs 为企业客服、金融和运营构建生产级 AI Agent 系统。如果你的 Agent 架构处于集成层且想要技术评审,请联系 hello@dextralabs.com