Skip to content

Quickstart

Terminal window
npm install agent-lattice zod

The examples use DeepSeek’s Anthropic-compatible endpoint.

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("Say hello in one sentence.")) {
console.log(message);
}

agent.query() returns an async iterator of SDK events. Use it when you want to observe the agent as it runs: initialization, assistant messages, streaming deltas, tool calls, tool results, and the final result.

agent.prompt() runs the same agent loop and resolves with only the final result message. It is the easiest API when your application does not need intermediate events.

const result = await agent.prompt("What is 2+2?");
console.log(result.result);

For image or document input, pass a content block array instead of embedding media in a string. The SDK forwards Anthropic-compatible image and document blocks through the agent loop.

const result = await agent.prompt([
{ type: "text", text: "Summarize this screenshot." },
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: imageBase64,
},
},
]);
console.log(result.result);

query() and prompt() describe how your app consumes SDK output. The stream option controls whether the underlying model request uses streaming.

// Model streaming + SDK event streaming.
for await (const message of agent.query("Write a short haiku.", {
stream: true,
})) {
console.log(message);
}
// Non-streaming model request, while still consuming SDK events.
for await (const message of agent.query("Write a short haiku.", {
stream: false,
})) {
console.log(message);
}
// Non-streaming model request + final result only.
const result = await agent.prompt("Write a short haiku.", {
stream: false,
});

Use outputFormat for schema-constrained JSON output:

const jsonResult = await agent.prompt("Return JSON only.", {
outputFormat: "json",
});
const result = await agent.prompt("Return the answer to 2 + 2.", {
outputFormat: {
type: "json_schema",
schema: {
type: "object",
properties: {
answer: { type: "number" },
},
required: ["answer"],
additionalProperties: false,
},
},
});

When outputFormat is set, the SDK sends structured output parameters to the provider and returns the final text unchanged. Parse or validate the returned JSON in your application when you need a typed value.

In short: query() is the event-stream API, prompt() is the final-result API, { stream: true | false } controls model streaming, and outputFormat requests JSON or provider-supported schema output.

When you add custom tools, import the tool helper explicitly:

import { createAgent, tool } from "agent-lattice";

Then continue with Tools.