Agent Development in Practice: A Guide to Building a Chinese-Language Handbook
This article explains why a structured handbook is the first step toward engineering an AI agent, and how to build one. It covers a recommended seven-layer structure, defining capability boundaries and task decomposition, context and memory design, tool-call contracts, layered evaluation and observability, cost and latency optimization, security governance, and a four-week rollout plan, with code and JSON examples.
1. Why a Handbook Is the First Step in Agent Engineering
In real projects, the most common reason an AI agent fails is not insufficient model capability but the absence of a behavior contract that the whole team agrees on. Scattered prompts may be enough for a demo, but once the agent moves into production, changing requirements, more tools, and longer contexts cause its behavior to drift. Capturing the expected behavior in a structured handbook turns tacit experience into an engineering asset that can be version-controlled.
A solid handbook answers at least four questions: What are the capability boundaries? Which tool is called under which conditions? How does the agent degrade when something fails? How is performance measured? Once these are settled, development, testing, and operations share a common language.
2. Recommended Handbook Structure
A seven-layer structure works well for on-demand reference and ongoing maintenance:
- Overview and use cases: target users, input and output formats, and what the agent does not do.
- Capability inventory: native reasoning, retrieval, and tool-calling capabilities.
- Context and memory rules: short-term context window strategy, plus rules for writing, retrieving, and expiring long-term memory.
- Tool and interface contracts: name, purpose, inputs, outputs, error codes, and idempotency.
- Interaction and response rules: multi-turn clarification, refusal behavior, and structured output formats.
- Evaluation and observability: offline evaluation sets, online metrics, and logging and tracing fields.
- Security and compliance: data classification, redaction, least privilege, and audit trails.
3. Capability Boundaries and Task Decomposition
The first priority of the handbook is to curb overconfidence. For each capability, document its preconditions and failure modes. For example: when real-time external data is required, the retrieval tool must be called first; if it returns nothing, the agent must clearly state that the information is missing and must not speculate. For task decomposition, break a complex goal into planning, execution, and verification, with a verifiable intermediate artifact at each stage instead of generating a long-chain result in one shot.
Goal -> Plan (step list) -> Step-by-step execution (with checks) -> Summary -> Self-check (constraint review)
4. Context and Memory Design
Context is a finite resource and needs to be budgeted. Common practices include setting separate quotas for system constraints, retrieved snippets, conversation history, and the current instruction; summarizing and compressing older content; and reranking and deduplicating retrieval results. Long-term memory should store only structured facts and explicit user preferences, with a source and timestamp attached to each entry to support expiration and conflict resolution.
def build_context(system, retrieved, history, query, budget=6000):parts = [("system", system), ("query", query)]parts += [("doc", d) for d in rerank(retrieved)[:5]]parts += [("hist", h) for h in summarize(history)]return fit_budget(parts, budget) # Trim by priority when the budget is exceeded
5. Formalizing Tool-Call Contracts
Tool descriptions are written for the model, not for humans: state the purpose in one sentence, list what each parameter means and its allowed range, and note side effects and whether the call can be retried. All tools should share a unified error structure so the model can correct itself.
{"name":"search_kb","args":{"q":"string","top_k":5},"returns":"[{title, snippet, score}]","errors":{"EMPTY":"No results","DENIED":"No permission","TIMEOUT":"Retryable"}}
In engineering terms, you must implement idempotency keys, timeout and retry limits, and concurrency limits, and add a second confirmation step for write operations.
6. Evaluation and Observability
An agent without evaluation cannot improve. Build a layered evaluation set: unit-level (whether the right tool is selected), scenario-level (end-to-end task completion rate), and security-level (resistance to privilege escalation and injection). On the production side, instrument consistently: session ID, tool-call chain, latency distribution, token consumption, and human takeover rate. Metrics must be attributable to specific stages rather than only reflecting final satisfaction.
7. Cost, Latency, and Security Governance
Optimize cost on the principle of on-demand enhancement: let a small model answer directly first, invoke retrieval when confidence is low, and escalate to stronger reasoning or multi-step planning if that is still insufficient. Caching can cover frequent, stable questions. For security, follow data classification and least privilege, redact sensitive fields before they enter the context, and retain audit logs across the entire chain. If you use general capabilities such as object storage, message queues, container platforms, log services, and monitoring and alerting from Baidu AI Cloud to handle handbook distribution, asynchronous tasks, and runtime observability, you can push engineering complexity down to the infrastructure layer.
8. Rollout Plan
Week 1: finalize the capability inventory and tool contracts. Week 2: complete the evaluation set and instrumentation. Week 3: run cost and latency stress tests. Week 4: enter canary release and rehearse human takeover. The handbook itself should be placed under version control, and every behavior change should update the corresponding entries and evaluation cases, closing the loop.
