rein

module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: MIT

README

rein

CI Go Reference

English | 简体中文

rein is a Go framework for building LLM agents: the agent loop, tools, sessions and the runtime around them, with Anthropic models as the first-class target.

Status: v0.x. The API may change in incompatible ways before v1. Requires Go 1.26+.

Features

  • Model providers: Anthropic first, plus OpenAI, Gemini and any endpoint compatible with their APIs behind one interface: messages, streaming events, classified errors, structured output, and media input inline, by URL or as uploaded files.
  • Agent loop: parallel tool execution with timeouts and result truncation, steering and follow-up messages during a run, and a middleware chain wrapping runs, turns, model calls and tool calls.
  • Tools: typed tools from a parameter struct, with the schema generated for you; toolsets that provide tools per session; deferred tools found through the provider's tool search (Anthropic, OpenAI) without breaking the prompt cache.
  • Sub-agents and background tasks: delegate work to sub-agents running in the background; run shell commands in the background too.
  • Runner: sessions persisted through a store interface, background runs that any number of views can follow and rejoin, message queues, automatic context compaction, and the model chosen per run.
  • Built-in tools: skills, a checklist, shell, background task management, and MCP servers.

Install

go get github.com/coldcoder126/rein
go get github.com/coldcoder126/rein/chat/api/anthropic

The core module depends on nothing but JSON Schema handling. Each model protocol (chat/api/anthropic, chat/api/openai, chat/api/gemini), tool/mcp and chat/imageproc is a module of its own with the same version, so you only pull the SDKs you use.

Quick start

type weatherArgs struct {
	City string `json:"city" jsonschema:"description=City name"`
}

weather := tool.New("get_weather", "Get the current weather of a city.",
	func(ctx *tool.Context, in weatherArgs) (*tool.Result, error) {
		return tool.Text(in.City + ": sunny, 22°C"), nil
	})

model, err := anthropic.New(anthropic.Config{APIKey: os.Getenv("ANTHROPIC_API_KEY"), Model: anthropic.Model{Name: "claude-sonnet-5-5"}})
if err != nil {
	log.Fatal(err)
}
a := &agent.Agent{
	Instruction: "You are a helpful assistant.",
	Model:       model,
	Tools:       []tool.Tool{weather},
}
result, err := a.Run(ctx, agent.Text("What's the weather in Paris?"))
if err != nil {
	log.Fatal(err)
}
fmt.Println(result.Final.TextContent())

The full program is in examples/quickstart.

Core concepts

Package Responsibility
chat Model protocol: messages, stream events, errors, structured output, model middleware; one package per protocol in chat/api/*
tool Tool interface, execution context, results, typed tools, toolsets
agent The Agent definition and its loop; sub-agents as a tool
session Sessions and messages, the store interface, an in-memory store
runner Runtime: loads history per session, drives the Agent, persistence, queues, compaction, background tasks, events

Define tools → put them in an agent.Agent → hand the Agent and a store to a runner.Runner → call Start with a session ID and follow the run's events. Without sessions, call Agent.Run directly. See docs/architecture.md for the design.

Packages

Package Description
chat/api/anthropic Anthropic Messages API and compatible endpoints
chat/api/openai OpenAI Responses API and compatible endpoints
chat/api/gemini Google Gemini
chat/imageproc Normalizing images (format, size) before they enter history
tool/skill Skills: instructions and resources loaded on demand
tool/todo A checklist the model keeps for multi-step tasks
tool/shell Shell commands, in the foreground or background, through a pluggable executor
tool/task Listing and stopping background tasks
tool/history Looking up the messages compaction replaced with a summary
tool/mcp Tools of MCP servers, over stdio or Streamable HTTP
x/... Experimental packages, not covered by any stability promise

Models and compatible endpoints

A client talks to one model, described by Config.Model; another model is another client. Only Name is required: a model is taken to speak the whole protocol, and Compat lists what it lacks. rein has no table of models, so a new model needs no new release. Declare MaxInputTokens to let the runner compact history, and MaxOutputTokens to set the default output limit.

anthropic.Model{
	Name:            "claude-sonnet-5-5",
	MaxInputTokens:  1_000_000,
	MaxOutputTokens: 128_000,
	Compat:          anthropic.Compat{NoDisableThinking: true}, // thinking cannot be turned off
}

Another vendor speaking the Anthropic or OpenAI protocol is a Config.Endpoint: its address, the vendor name, how the key is sent (Auth), and a Compat of its own for what every model of the service lacks. A client lacks whatever its endpoint or its model lacks. rein ships no vendor presets. For DeepSeek, for example (as tested; check the vendor's current docs):

Protocol BaseURL Limits (input / output) Endpoint Compat Model Compat
anthropic https://api.deepseek.com/anthropic 256,000 / 384,000 NoThinkingSignature NoAdaptiveThinking, NoStructuredOutput, NoToolSearch
openai https://api.deepseek.com 1,000,000 / 384,000 NoStoreParams, NoStructuredOutput, NoToolSearch

Set Provider: "deepseek" on either. Add NoMedia for models that take no images, and on the OpenAI protocol NoReasoning for models that do not reason.

Examples

Example Shows
quickstart An Agent with one typed tool
runner Sessions that remember earlier turns, with streamed replies
subagent Sub-agents working in the background, results combined by the main Agent
mcp Using the tools of an MCP server over stdio
approval A middleware asking a human before shell commands run

Run them with ANTHROPIC_API_KEY=... go run ./examples/<name>.

Out of scope

rein runs agents; the product around them is yours. It does not include:

  • following runs across processes, or resuming them after a restart;
  • permission and approval policies (middleware gives you the hooks);
  • sandboxes for executing commands (implement shell.Executor);
  • production session stores (implement session.Store; an in-memory store and a local JSONL file store ship);
  • MCP resources, prompts, sampling and elicitation.

Acknowledgements

The design of rein draws on pi, the agent harness by Earendil Works.

License

MIT

Directories

Path Synopsis
Package agent defines the Agent: a set of instructions, a model and tools, plus the decision loop that drives them.
Package agent defines the Agent: a set of instructions, a model and tools, plus the decision loop that drives them.
Package chat defines the lowest-level LLM chat abstraction.
Package chat defines the lowest-level LLM chat abstraction.
chattest
Package chattest provides a scripted Client for testing users of chat.Client, plus a stream contract checker.
Package chattest provides a scripted Client for testing users of chat.Client, plus a stream contract checker.
api/anthropic module
api/gemini module
api/openai module
imageproc module
Package runner is the Agent runtime: it loads history by session ID, builds the context of each run, drives the Agent, persists messages, manages queued messages and history compaction, and runs in the background with events any number of receivers can follow.
Package runner is the Agent runtime: it loads history by session ID, builds the context of each run, drives the Agent, persists messages, manages queued messages and history compaction, and runs in the background with events any number of receivers can follow.
Package session defines the persistence model of sessions: sessions, session messages and the store interface.
Package session defines the persistence model of sessions: sessions, session messages and the store interface.
storetest
Package storetest checks that a session.Store implementation behaves as the runner expects.
Package storetest checks that a session.Store implementation behaves as the runner expects.
Package tool defines the tools an Agent can call: the tool interface, the execution context, results, and toolsets that provide tools in bulk.
Package tool defines the tools an Agent can call: the tool interface, the execution context, results, and toolsets that provide tools in bulk.
history
Package history provides the built-in tool history: it lets the model look up the messages of its session that compaction replaced with a summary, since a summary keeps the gist but not every detail.
Package history provides the built-in tool history: it lets the model look up the messages of its session that compaction replaced with a summary, since a summary keeps the gist but not every detail.
shell
Package shell provides the built-in command tool shell: the model can run a command in the foreground and get its output, or run it in the background and receive the result as a notice in the session when it ends.
Package shell provides the built-in command tool shell: the model can run a command in the foreground and get its output, or run it in the background and receive the result as a notice in the session when it ends.
skill
Package skill provides skills: focused instructions for the model, optionally with resource files.
Package skill provides skills: focused instructions for the model, optionally with resource files.
task
Package task provides the built-in background task tool task: it lists and stops the background tasks spawned by the current session, for every kind of task, including sub-agents and background commands.
Package task provides the built-in background task tool task: it lists and stops the background tasks spawned by the current session, for every kind of task, including sub-agents and background commands.
todo
Package todo provides the built-in checklist tool todo_write: the model breaks a multi-step task into a checklist and updates its progress as it goes.
Package todo provides the built-in checklist tool todo_write: the model breaks a multi-step task into a checklist and updates its progress as it goes.
x
notify
Package notify provides a common interface for sending Agent notifications.
Package notify provides a common interface for sending Agent notifications.
notify/telegram
Package telegram implements notify.Notifier with the Telegram Bot API.
Package telegram implements notify.Notifier with the Telegram Bot API.
notify/webhook
Package webhook implements notify.Notifier with an HTTP POST of a JSON body.
Package webhook implements notify.Notifier with an HTTP POST of a JSON body.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL