# Program

> Write inferlets for custom decoding, tool calls, structured output, sessions, and agents.

Source: https://pie-project.org/docs/program  

When a stateless chat endpoint is not enough (for example, when you need multi-step agents, constrained output, branching search, or stateful sessions), you write an **inferlet**.

As the figure below shows, an inferlet is a short program that runs inside the engine next to the model, while the runtime batches its forward passes with everyone else's.

> **Figure.** An inferlet sits between the client and the engine. It talks to the client, submits forward passes to the engine, and can call tools over HTTP itself. The client sends one request. Everything else, including tool calls, happens next to the model.

You do not need to write GPU kernels or manage batching. Every page in this section starts from the same `generate` loop and adds one concept at a time.

- [Your first inferlet](https://pie-project.org/docs/program/first-inferlet): Stream a chat reply in about 30 lines of Python.

- [Chat, tools & reasoning](https://pie-project.org/docs/program/chat-tools): Format chat turns, tool calls, and thinking blocks across models.

- [Structured output](https://pie-project.org/docs/program/structured-output): Enforce JSON Schema, regex, and EBNF grammars.

- [Sessions](https://pie-project.org/docs/program/sessions): Keep KV cache state alive across multi-turn conversations.

- [Branching](https://pie-project.org/docs/program/branching): Fork one working set into parallel continuations.

- [Agents](https://pie-project.org/docs/program/agents): Run tool loops inside the engine without network round trips.

- [Ship it](https://pie-project.org/docs/program/ship): Install inferlets on a server and call them from clients.

Inferlets can be written in **Python**, **JavaScript**, or **Rust**. These pages use Python; the [Cookbook](https://pie-project.org/docs/cookbook) has versions of the main examples in all three languages.

> **Go deeper:** [What a forward pass can do](https://pie-project.org/docs/invent). Lanes, attention masks, adapters, and device epilogues: the primitives under every helper.
