快速开始
npm install agent-lattice zod发起一次提问
Section titled “发起一次提问”下面示例使用 DeepSeek 的 Anthropic 兼容接口。你也可以换成 Anthropic 官方 API。
import { createAgent } from "agent-lattice";
const agent = createAgent({ apiKey: process.env.DEEPSEEK_API_KEY, baseURL: "https://api.deepseek.com/anthropic", model: "deepseek-v4-flash",});
for await (const message of agent.query("用一句话介绍你自己。")) { console.log(message);}agent.query() 返回 async iterator。你可以边跑边消费初始化事件、模型回复、工具结果、流式事件和最终结果。
只拿最终结果
Section titled “只拿最终结果”agent.prompt() 会跑完同一个 agent loop,然后只返回最后的 result 消息。如果你的应用不关心中间事件,这是最简单的 API。
const result = await agent.prompt("2 + 2 等于多少?");console.log(result.result);发送多模态输入
Section titled “发送多模态输入”图片或文档输入要传 content block 数组,不要塞进普通字符串。SDK 会在 agent loop
中原样转发 Anthropic 兼容的 image 和 document block。
const result = await agent.prompt([ { type: "text", text: "请总结这张截图。" }, { type: "image", source: { type: "base64", media_type: "image/png", data: imageBase64, }, },]);
console.log(result.result);选择流式行为
Section titled “选择流式行为”query() 和 prompt() 描述的是你的应用如何消费 SDK 输出;stream 选项控制的是底层模型请求是否使用流式调用。
// 模型流式调用 + SDK 事件流。for await (const message of agent.query("写一首短诗。", { stream: true,})) { console.log(message);}// 模型非流式调用,但仍然逐个消费 SDK 事件。for await (const message of agent.query("写一首短诗。", { stream: false,})) { console.log(message);}// 模型非流式调用 + 只拿最终结果。const result = await agent.prompt("写一首短诗。", { stream: false,});需要让模型按 JSON Schema 输出时,传入 outputFormat:
const jsonResult = await agent.prompt("只返回 JSON。", { outputFormat: "json",});
const result = await agent.prompt("返回 2 + 2 的答案。", { outputFormat: { type: "json_schema", schema: { type: "object", properties: { answer: { type: "number" }, }, required: ["answer"], additionalProperties: false, }, },});设置 outputFormat 后,SDK 会把结构化输出参数发送给 provider,并原样返回最终文本。需要类型化结果时,请在应用层解析或验证返回的 JSON。
简单记:query() 是事件流 API,prompt() 是最终结果 API,{ stream: true | false } 控制模型是否流式输出,outputFormat 请求 JSON 或 provider 支持的 schema 输出。
接入自定义工具时,tool 需要显式 import:
import { createAgent, tool } from "agent-lattice";- 需要接入自定义函数,阅读 工具。
- 需要文件和 Shell 能力,阅读 内置工具。
- 需要一次性子智能体协作,阅读 Supervisor 子智能体委派。
- 需要持久团队协作,阅读 Mailbox Team 持久团队。