02. Agent Engineer 核心知识地图

Agent Engineer 的工作不是「调用一个模型」,而是构建一个能理解任务、调用工具、处理状态、接受反馈、持续评估和稳定上线的系统。

1. LLM API 基础

必须掌握:

  • messages / roles / system prompt
  • streaming
  • structured output
  • token 和 context window
  • temperature / top_p 等采样参数
  • rate limit
  • latency / cost
  • model fallback

验收标准:

  • 能解释一次请求的 token 消耗。
  • 能实现 streaming UI。
  • 能让模型返回结构化 JSON,并处理 schema validation 失败。

2. Prompt 与消息设计

Prompt 不是一次性文案,而是产品逻辑的一部分。

需要掌握:

  • system prompt 和 task prompt 的边界
  • few-shot examples
  • tool instruction
  • output contract
  • refusal / no-answer policy
  • versioning
  • prompt regression

实践建议:

  • 每次改 prompt 都记录版本。
  • 重要 prompt 必须配 eval dataset。
  • 不要只靠单个样例判断 prompt 好坏。

3. Tool Calling

Tool calling 是 Agent 工程的核心分水岭。一个工具不是普通函数,它是给模型理解和选择的能力边界。

需要掌握:

  • 工具命名
  • 工具描述
  • 参数 schema
  • 错误返回格式
  • 权限检查
  • 幂等性
  • retry
  • destructive action confirmation

好的工具应该:

  • 单一职责
  • 输入清晰
  • 输出稳定
  • 错误可解释
  • 不暴露不必要权限

参考:LangChain JS ToolsAnthropic: Writing effective tools for agents

4. RAG

RAG 解决的是「模型不知道或不能记住的知识」问题。

核心链路:

  1. 文档加载
  2. 清洗和切分
  3. embedding
  4. 向量检索
  5. rerank
  6. context assembly
  7. grounded answer
  8. citation
  9. no-answer
  10. retrieval eval

常见失败:

  • chunk 太大导致噪音多
  • chunk 太小导致上下文断裂
  • query 不适合直接 embedding
  • 检索到了相关但不充分的内容
  • 模型忽略检索证据
  • 没有 no-answer 策略

参考:LangChain Retrieval

5. Memory 和 State

Agent 的记忆不是简单把聊天记录塞回 prompt。

常见层次:

  • short-term conversation history
  • thread state
  • summarized memory
  • user preference memory
  • task state
  • external persistent memory

需要区分:

  • 这次任务需要的上下文
  • 用户长期偏好
  • 可从数据库重新查询的信息
  • 不应该保存的敏感信息

参考:LangChain Memory Concepts

6. Workflow 和 Multi-Agent

不要一开始就做复杂 multi-agent。先判断任务是否需要:

  • 明确步骤
  • 状态持久化
  • 可恢复执行
  • 人工审批
  • 分支路由
  • evaluator loop
  • 多工具协调

常见模式:

  • prompt chaining
  • routing
  • parallelization
  • orchestrator-worker
  • evaluator-optimizer
  • autonomous agent loop

参考:Anthropic: Building Effective AgentsLangGraph Workflows and Agents

7. Evaluation

Eval 是 Agent 工程最重要的工程纪律之一。

需要掌握:

  • offline eval
  • online eval
  • golden dataset
  • regression dataset
  • LLM-as-judge
  • human annotation
  • pairwise eval
  • trajectory eval
  • tool-use eval
  • cost / latency eval

好的 eval 问题不是「回答是否好」,而是更具体:

  • 是否调用了正确工具?
  • 是否引用了正确来源?
  • 是否拒绝了不该回答的问题?
  • 是否在缺少证据时选择 no-answer?
  • 是否在执行动作前要求确认?

参考:LangSmith EvaluationAnthropic: Demystifying evals for AI agents

8. Observability

Agent 的可观测性至少包括:

  • trace
  • span
  • prompt
  • model response
  • tool input / output
  • retrieval chunks
  • latency
  • token
  • cost
  • user feedback
  • metadata

目标不是「有日志」,而是能回答:

  • 为什么这次回答错了?
  • 哪一步最慢?
  • 哪个工具最常失败?
  • 哪类问题最容易 hallucinate?
  • 哪个版本上线后质量下降?

参考:LangSmith Observability

9. Guardrails 和安全

Agent 工程里的安全不是附加项。

重点包括:

  • prompt injection
  • data exfiltration
  • PII redaction
  • output validation
  • tool permission
  • scoped credentials
  • human approval
  • audit log
  • policy-based refusal

参考:LangChain GuardrailsOpenAI Guardrails Cookbook

10. 成本、延迟和部署

Agent 产品上线后,成本和延迟会快速成为核心问题。

优化方向:

  • streaming
  • parallel tool calls
  • prompt compression
  • caching
  • smaller model routing
  • retrieval limit
  • batch eval
  • trace sampling
  • model fallback
  • async workflow

参考:OpenAI Latency Optimization