19
Agent 深入
Agent advanced: design multi-tool, multi-step agents with planning, safety, and stability.
1) Agent Styles
- ReAct: interleave reasoning + tool use, good for search/code tasks.
- Plan-and-execute: plan first, then execute steps; stable for longer tasks.
- Tree of Thoughts / Reflexion: explore multiple options, self-critique, pick best.
- Router agents: choose specialized sub-agents by intent/domain.
2) Tooling Strategy
- Tool schema: explicit params, types, constraints; prevent free-text tool calls.
- Tool selection: require rationale; limit tool list per task to reduce mistakes.
- Sandboxing: code exec/search in isolated env; resource/time limits.
- Idempotent tools where possible; otherwise guard with locks.
3) Planning & Memory
- Short-term: keep recent steps concise; prune verbose logs.
- Long-term: store key facts/decisions; retrieve by intent or doc ID.
- Checkpoints: allow resume after failure; persist plan + progress.
4) Safety & Policy
- Guardrails before/after tool calls (PII, dangerous actions).
- Allowlist external domains/APIs; blockwrites unless explicitly permitted.
- Human approval steps for risky actions (emails, transactions, prod changes).
5) Quality Controls
- Stop conditions: max steps/tool calls/time; detect loops.
- Self-critique: ask agent to verify outputs vs. instructions and citations.
- Dual-model verification: cheap model executes, stronger model reviews.
6) Observability
- Trace per run: steps, chosen tools, inputs/outputs, duration, errors.
- Metrics: success rate, avg steps, tool error rate, cancel rate.
- Replay: store deterministic inputs; enable re-run with same seed/config.
7) Minimal Skeleton (pseudo)
while not done:
plan = agent.decide(observation, tools)
if plan.type == "tool":
result = run_tool(plan.tool, plan.args, sandbox=true)
else:
result = agent.respond()
observation.append(result)
if stop_condition(): break
8) Practice
- Build a ReAct agent with 2 tools (search + calculator); add max-steps + timeout.
- Add a self-critique step: verify answer cites the tool outputs.
- Log each step to a trace store (time, tool, args, tokens) and surface a run report.