loop

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 7 Imported by: 3

README

axon-loop

A provider-agnostic conversation loop for LLM-powered agents. Part of lamina — each axon package can be used independently.

Handles message exchange, tool call dispatch, and streaming — with no HTTP, persistence, or UI concerns.

Install

go get github.com/benaskins/axon-loop@latest

Requires Go 1.24+.

Usage

Implement the LLMClient interface for your LLM backend, define tools, and run:

result, err := loop.Run(ctx, loop.RunConfig{
    Client:   myLLMClient,
    Messages: messages,
    Tools:    toolDefs,
    Callbacks: loop.Callbacks{
        OnToken: func(token string) { fmt.Print(token) },
    },
})
Key types
  • LLMClient — interface abstracting communication with any LLM backend
  • Request / Response — provider-agnostic request and streamed response
  • Message — a single message in a conversation
  • ToolCall — an LLM's decision to invoke a tool
  • Run() — executes the agent conversation loop with tool dispatch

License

MIT — see LICENSE.

Documentation

Overview

Package loop provides a provider-agnostic conversation loop for LLM-powered agents. It handles message exchange, tool call dispatch, and streaming — with no HTTP, persistence, or UI concerns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callbacks

type Callbacks struct {
	OnToken    func(token string)
	OnThinking func(token string)
	OnToolUse  func(name string, args map[string]any)
	OnDone     func(durationMs int64)
}

Callbacks receives streaming events from the conversation loop. All fields are optional — nil callbacks are skipped.

type LLMClient added in v0.2.0

type LLMClient interface {
	Chat(ctx context.Context, req *Request, fn func(Response) error) error
}

LLMClient abstracts communication with an LLM backend. Implementations translate to/from provider-specific APIs (e.g. Ollama, OpenAI, Anthropic).

type Message

type Message struct {
	Role      string
	Content   string
	Thinking  string
	ToolCalls []ToolCall
}

Message represents a single message in a conversation.

type Request added in v0.3.0

type Request struct {
	Model         string
	Messages      []Message
	Tools         []tool.ToolDef
	Stream        bool
	Think         *bool
	Options       map[string]any
	MaxIterations int // Maximum tool-call loop iterations. Defaults to 20 if 0.
}

Request is a provider-agnostic request to an LLM.

type Response added in v0.3.0

type Response struct {
	Content   string
	Thinking  string
	Done      bool
	ToolCalls []ToolCall
}

Response is a provider-agnostic streamed response chunk from an LLM.

type Result

type Result struct {
	Content  string
	Thinking string
}

Result is the final output of a conversation loop run.

func Run

func Run(ctx context.Context, client LLMClient, req *Request, tools map[string]tool.ToolDef, toolCtx *tool.ToolContext, cb Callbacks) (*Result, error)

Run executes a conversation loop: sends messages to the LLM, streams the response, executes tool calls, and repeats until no more tool calls are made.

tools and toolCtx may be nil for simple chat without tool support.

type ToolCall

type ToolCall struct {
	Name      string
	Arguments map[string]any
}

ToolCall represents an LLM's decision to invoke a tool.

Jump to

Keyboard shortcuts

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