Skip to content

Examples

Standalone scripts for common llm-chat use cases. Most pages contain one TypeScript program; the system prompt page collects independent snippets.

Setup

The examples use Node.js 22 or newer and ESM. Install the engine and the tool primitives once:

npm install @johannes.latzel/llm-chat @johannes.latzel/llm-chat-message @johannes.latzel/llm-chat-tool

This package ships no provider. Install the OpenAI provider package @johannes.latzel/llm-chat-openai, or subclass ChatService yourself. Most examples stay provider-neutral and declare a service: ChatService value instead of constructing a concrete service; Quick start constructs OpenAIChatService. See Adding a new provider for a skeleton implementation.

Each example imports from the package that owns the symbol: engine types from @johannes.latzel/llm-chat, messages and roles from @johannes.latzel/llm-chat-message, and tools, results and tool hooks from @johannes.latzel/llm-chat-tool. Nothing is re-exported by the engine.

Set the generic LLM_CHAT_* variables listed in Environment variables. Model names, credentials and transport are provider configuration.

The examples are ESM modules and use top-level await. Run a script with a TypeScript runner such as tsx:

npx tsx example.ts

Basics

Tools

  • Tool calling: let the model call a registered tool and feed the result back.

Agents and runtime control

  • Agent loop: continue until a tool confirms the answer, with a round cap.
  • Hooks: observe streams, messages, tools, and the send loop.
  • Service hooks: inject messages and retry from the send loop.
  • JSON hooks: declarative retries and logging from .json files.
  • Interruption: abort a request and resend with a correction.
  • Context management: estimate context size and compact history.

Sessions and operations

  • Persistence: save a transcript and resume it in a new process.
  • Testing: run a deterministic tool-call flow without a network.

Moved to another package

Tool-focused examples and guides now live in @johannes.latzel/llm-chat-tool:

See also