voltagent-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVoltAgent Best Practices
VoltAgent 最佳实践
Quick reference for VoltAgent conventions and patterns.
VoltAgent 规范与模式速查手册。
Choosing Agent or Workflow
选择 Agent 还是 Workflow
| Use | When |
|---|---|
| Agent | Open-ended tasks that require tool selection and adaptive reasoning |
| Workflow | Multi-step pipelines with explicit control flow and suspend/resume |
| 使用场景 | 适用时机 |
|---|---|
| Agent | 需要工具选择和自适应推理的开放式任务 |
| Workflow | 具有显式控制流和暂停/恢复功能的多步骤流水线 |
Layout
项目布局
src/
|-- index.ts
|-- agents/
|-- tools/
`-- workflows/src/
|-- index.ts
|-- agents/
|-- tools/
`-- workflows/Quick Snippets
快速代码片段
Basic Agent
基础 Agent
typescript
import { Agent } from "@voltagent/core";
const agent = new Agent({
name: "assistant",
instructions: "You are helpful.",
model: "openai/gpt-4o-mini",
});Model format is (for example or ).
provider/modelopenai/gpt-4o-minianthropic/claude-3-5-sonnettypescript
import { Agent } from "@voltagent/core";
const agent = new Agent({
name: "assistant",
instructions: "You are helpful.",
model: "openai/gpt-4o-mini",
});模型格式为(例如或)。
provider/modelopenai/gpt-4o-minianthropic/claude-3-5-sonnetBasic Workflow
基础 Workflow
typescript
import { createWorkflowChain } from "@voltagent/core";
import { z } from "zod";
const workflow = createWorkflowChain({
id: "example",
input: z.object({ text: z.string() }),
result: z.object({ summary: z.string() }),
}).andThen({
id: "summarize",
execute: async ({ data }) => ({ summary: data.text }),
});typescript
import { createWorkflowChain } from "@voltagent/core";
import { z } from "zod";
const workflow = createWorkflowChain({
id: "example",
input: z.object({ text: z.string() }),
result: z.object({ summary: z.string() }),
}).andThen({
id: "summarize",
execute: async ({ data }) => ({ summary: data.text }),
});VoltAgent Bootstrap
VoltAgent 启动代码
typescript
import { VoltAgent } from "@voltagent/core";
import { honoServer } from "@voltagent/server-hono";
new VoltAgent({
agents: { agent },
workflows: { workflow },
server: honoServer(),
});typescript
import { VoltAgent } from "@voltagent/core";
import { honoServer } from "@voltagent/server-hono";
new VoltAgent({
agents: { agent },
workflows: { workflow },
server: honoServer(),
});Memory Defaults
内存默认配置
- Use for a shared default across agents and workflows.
memory - Use or
agentMemorywhen defaults need to differ.workflowMemory
- 使用在Agent和工作流之间共享默认配置。
memory - 当默认配置需要区分时,使用或
agentMemory。workflowMemory
Server Options
服务器选项
- Use for Node HTTP servers.
@voltagent/server-hono - Use as an alternative Node server provider.
@voltagent/server-elysia - Use provider for fetch runtimes (Cloudflare, Netlify).
serverless
- 使用搭建Node HTTP服务器。
@voltagent/server-hono - 可选择作为替代的Node服务器提供商。
@voltagent/server-elysia - 对于Fetch运行时(Cloudflare、Netlify),使用提供商。
serverless
Observability Notes
可观测性说明
- Use or
VoltOpsClientfor tracing.createVoltAgentObservability - VoltAgent will auto-configure VoltOps if and
VOLTAGENT_PUBLIC_KEYare set.VOLTAGENT_SECRET_KEY
- 使用或
VoltOpsClient进行追踪。createVoltAgentObservability - 如果设置了和
VOLTAGENT_PUBLIC_KEY,VoltAgent将自动配置VoltOps。VOLTAGENT_SECRET_KEY
Recipes
实践示例
Short best-practice recipes live in the embedded docs:
packages/core/docs/recipes/- Search:
rg -n "keyword" packages/core/docs/recipes -g"*.md" - Read:
cat packages/core/docs/recipes/<file>.md
简短的最佳实践示例位于嵌入式文档中:
packages/core/docs/recipes/- 搜索:
rg -n "keyword" packages/core/docs/recipes -g"*.md" - 阅读:
cat packages/core/docs/recipes/<file>.md
Footguns
注意事项
- Do not use inside VoltAgent packages. Use
JSON.stringifyfromsafeStringify.@voltagent/internal
- 不要在VoltAgent包内使用,请使用
JSON.stringify中的@voltagent/internal。safeStringify