Skip to main content
Every TypeScript snippet on this page is a complete, standalone ES module (.mts). They are type-checked and executed against a locally packed build of the SDK by npx tsx scripts/verify-docs-snippets.ts, so they are kept in sync with the real API. All of them run as-is with the SDK’s built-in mock provider - no API key needed - except the first, which talks to a real model and is therefore only type-checked.

Start a new project

The fastest way in is one command, which creates a runnable project (an agent, an example tool, an offline test, a .env.example for your provider), installs its dependencies and runs git init:
npm create lousho-agent my-agent is equivalent, and so is npx @lousho/build-ai-agent init my-agent when the SDK is not installed yet (a bare npx lousho only finds the lousho command once the SDK is in your node_modules). Without arguments, init asks for the directory, provider and template; for scripts pass --yes (defaults: the minimal template and the provider whose API key variable is set, else OpenAI). Useful flags: --provider openai|anthropic|openrouter|ollama, --template minimal|tools|yaml, --package-manager npm|pnpm|yarn|bun, --no-install, --no-git, --force (write into a non-empty directory). lousho init --help lists them all. Prefer to add the SDK to an existing project? Install it by hand (see Installation):
That is the current ai major; for Ollama, use ai@^4.3.19 with ollama-ai-provider@^1.2.0 (see the pairings).

1. Hello world in five lines

createAgent() is the zero-config entry point: a provider/model string and instructions in, a { send } agent out. The API key is read from the provider’s conventional environment variable (OPENAI_API_KEY here; ANTHROPIC_API_KEY, OPENROUTER_API_KEY or OLLAMA_BASE_URL for the other providers).
Leave model out to let the environment decide: LOUSHO_MODEL (a provider/model string) if set, otherwise the first of OPENAI_API_KEY, ANTHROPIC_API_KEY, OPENROUTER_API_KEY, OLLAMA_BASE_URL that is present. If a key is missing or the prefix is misspelled, the error tells you exactly what to set or fix. instructions is optional; prompt is accepted as an alias for it.

2. When you need a custom provider

Pass a provider instance instead of model when you have your own LLMProvider, want the mock provider for tests, or need extra provider config. This one needs no API key:
resolveProvider('<provider>/<model>') is the function model uses under the hood; call it yourself when you want the provider object, as in this snippet, which uses OpenAI when OPENAI_API_KEY is set and falls back to the mock provider otherwise - the same pattern the runnable examples use.

3. Adding tools

Define a tool with defineTool(): the execute and needsApproval arguments are typed from the zod input, and the result goes straight into createAgent({ tools: [...] }). The mock provider simulates a tool call whenever the user message mentions a tool’s name, so this snippet exercises a real tool round trip without an LLM.
Names must be 1-64 characters of letters, digits, _ or -. The SDK also ships built-in tools (currentDateTool, dayNameTool, httpTool, …) that you pass keyed by name: tools: { current_date: currentDateTool }. Advanced: ToolRegistry. To share tools across agents or register raw ToolDescriptors, use registry.register(tool) for a defined tool or registry.register(name, descriptor) for a descriptor.

4. Full control: AgentBuilder + AgentExecutor

createAgent() is a thin wrapper over AgentBuilder and the static AgentExecutor.execute(). Use them directly only for what createAgent() does not take: temperature and maxTokens, and a TraceExporter for tracing (exporter, captureContent). createAgent() already takes maxSteps, limits, onEvent, approvals, store (checkpoints), hooks and compaction. AgentExecutor is a static API - there is no new AgentExecutor().

5. Declarative agents: spec files

An agent can also be described as plain data - an AgentSpec - and turned into a live agent with specToAgent(). The same shape can be written as a YAML or JSON file and loaded with loadSpec().
Saved as agent.yaml:
the same spec runs in the local dev server (chat UI at /, POST /chat, hot reload on save) and builds into a deployable server:

Next steps

  • Configuration - every spec field, provider env var and CLI flag.
  • Deployment - lousho build targets (Node server, Docker, Cloudflare Workers).
  • API Overview - the main exports and where to find full API reference.
  • Examples - runnable example agents.