• English
  • Structured output

    Free-text answers are fine for chat, but when your agent feeds another program you want a typed, validated result, not prose to parse. Set outputSchema — a Zod object schema — and the run's final answer is forced through it: the SDK registers a final_answer tool with your schema as its parameters, instructs the model to call it exactly once when done, validates the arguments, and surfaces them as result.output.

    Usage

    import { createLiteAgent } from "@lite-agent/sdk";
    import { anthropic } from "@lite-agent/provider";
    import { z } from "zod";
    
    const agent = createLiteAgent({
      model: anthropic(),
      modelName: "claude-sonnet-4-6",
      workdir: process.cwd(),
      outputSchema: z.object({
        name: z.string(),
        deps: z.number(),
      }),
    });
    
    const result = await agent.send("Summarize package.json");
    result.output; // { name: "…", deps: 42 } — validated against the schema

    query() accepts the same option. Its generator resolves to the LiteAgentResult, so drive it manually if you need result.output:

    import { query } from "@lite-agent/sdk";
    
    const run = query({
      prompt: "Summarize package.json",
      model: anthropic(),
      cwd: process.cwd(),
      outputSchema: z.object({ name: z.string(), deps: z.number() }),
    });
    
    let result;
    while (!(result = await run.next()).done) {
      // stream events as usual: result.value is an AgentEvent
    }
    result.value.output; // LiteAgentResult.output

    How it works

    1. A final_answer tool is registered with your schema as its parameter schema, so the model can only produce structurally valid arguments.
    2. A ## Final answer section is appended to the system prompt: the model must call final_answer exactly once when the task is complete, and only that call is read as the answer.
    3. The tool handler records the validated arguments for the current session; when the run resolves, they are attached as result.output.

    LiteAgentResult is RunResult & { output?: unknown }output is only present when outputSchema is set and the model produced the final answer.

    Details

    AspectBehavior
    Schema shapeMust be a Zod object schema; its fields become the final_answer parameters.
    ValidationArguments are validated by the schema before being recorded; malformed calls surface as ordinary tool errors the model can correct.
    result.outputThe validated arguments of the final_answer call for that session, attached to the LiteAgentResult returned by send() / query().
    Side effectsfinal_answer is declared with network: "none", filesystem: "none", sideEffects: "none" — it only records the answer.
    SubagentsoutputSchema is not inherited by subagents; each child returns plain text.

    See also

    • System prompt — how outputSchema extends the prompt with a ## Final answer section.
    • Subagents — why children don't inherit the schema.
    • Getting started — install and run your first agent.