Expand description
§Tower LLM
A powerful Tower-based framework for building multi-agent workflows with OpenAI LLMs. This SDK provides composable, type-safe components using Tower’s service architecture for maximum flexibility and performance.
§Core Concepts
- Agent: A Tower service that processes chat requests through an LLM with tools
- Tools: Type-safe functions that agents can call, with automatic schema generation
- Layers: Tower middleware for cross-cutting concerns (retry, timeout, observability)
- Static DI: All dependencies are injected at construction time - no runtime lookups
§Getting Started
Set your OpenAI API key in the OPENAI_API_KEY environment variable.
use tower_llm::{Agent, tool_typed, policies, CompositePolicy, run_user};
use async_openai::{config::OpenAIConfig, Client};
use schemars::JsonSchema;
use serde::Deserialize;
use std::sync::Arc;
use tower::{Service, ServiceExt};
#[derive(Debug, Deserialize, JsonSchema)]
struct AddArgs {
a: f64,
b: f64,
}
// Create OpenAI client
let client = Arc::new(Client::<OpenAIConfig>::new());
// Define a tool
let calculator = tool_typed(
"add",
"Add two numbers",
|args: AddArgs| async move {
Ok(serde_json::json!({ "sum": args.a + args.b }))
},
);
// Build an agent with per-agent instructions
let agent = Agent::builder(client)
.model("gpt-4o")
.instructions("You are a helpful math assistant")
.tool(calculator)
.policy(CompositePolicy::new(vec![policies::until_no_tool_calls()]))
.build();
// Use the agent with Tower's Service trait
let mut agent = agent;
// Send only a user message; the agent injects its instructions as a system message
let response = run_user(&mut agent, "What is 2 + 2?").await?;
println!("Agent: {:?}", response);Re-exports§
pub use error::AgentsError;pub use error::Result;pub use memory::Session;pub use result::RunResult;pub use result::RunResultWithContext;pub use result::StreamingRunResult;pub use sqlite_session::SqliteSession;
Modules§
- approvals
- Guardrails and approvals
- auto_
compaction - Auto-compaction layer for managing conversation context length
- budgets
- Budgets: tokens, time, and tool invocations
- codec
- Bijection codec between RunItem and raw OpenAI chat messages
- concurrency
- Parallel tool execution and concurrency controls
- error
- Error Handling for the Agents SDK
- groups
- Multi-agent orchestration and handoffs
- items
- Items (orientation)
- memory
- Session trait for compatibility with SqliteSession
- observability
- Observability: tracing and metrics
- policies
- Built-in policies
- provider
- Model provider abstractions (streaming and non-streaming)
- recording
- Recording and replay I/O surfaces
- resilience
- Resilience layers: timeout, retry, rate-limit, circuit-breaker
- result
- Result types for agent execution
- sessions
- Sessions and persistence (memory/replay) for the
nextstack - sqlite_
session - SQLite-Based Session Storage
- streaming
- Streaming step/agent variants
- tap
- Generic, zero-interference tap layer for observing requests, responses, and errors.
- validation
- Conversation validation utilities (pure, test-focused).
Structs§
- Agent
- Thin facade to build an agent stack from tools, model, and policy.
- Agent
Builder - Agent
Loop - Agent
Loop Layer - Layer to wrap a step service with an agent loop controlled by a policy.
- Agent
Run - Final run summary from the agent loop.
- Client
- Client is a container for config, backoff and http_client used to make API calls.
- Composite
Policy - Composite policy: stop when any sub-policy returns a stop reason.
- Create
Chat Completion Request - Create
Chat Completion Request Args - Builder for
CreateChatCompletionRequest. - Loop
State - Loop state visible to policies.
- OpenAI
Config - Configuration for OpenAI API
- Policy
- Chainable policy builder.
- Policy
Fn - Function-backed policy for ergonomic composition.
- Step
- One-step agent service parameterized by a routed tool service
S. - StepAux
- Auxiliary accounting captured per step.
- Step
Layer - Layer that lifts a routed tool service
Sinto aStep<S>service. - ToolDef
- Definition of a tool: function spec (for OpenAI) + service implementation.
- Tool
Invocation - Uniform tool invocation passed to routed tool services.
- Tool
Output - Uniform tool output produced by tool services.
- Tool
Router - Simple router service over tools using a name → index table.
Enums§
- Agent
Stop Reason - Stop reasons reported by the agent loop.
- Chat
Completion Request Message - Reasoning
Effort - Step
Outcome - Outcome of a single agent step.
- Tool
Join Policy - Join policy for parallel tool execution
Traits§
- Agent
Policy - Policy interface controlling loop termination.
- LLMInstruction
Provider - Layer
- Decorates a
Service, transforming either the request or the response. - Service
- An asynchronous function from a
Requestto aResponse. - Service
Ext - An extension trait for
Services that provides a variety of convenient adapters
Functions§
- run
- Convenience: run a prompt through an agent service.
- run_
user - Convenience: run a user message through an agent service. System instructions come from the agent.
- simple_
chat_ request - Build a simple chat request from plain strings.
- simple_
user_ request - Build a simple chat request with only a user message.
- tool_
typed - DX sugar: create a tool from a typed handler.