Expand description
Runtime-agnostic agent loop orchestration for sessions, turns, tools, and interrupts.
agentkit-loop is the central coordination layer in the agentkit workspace. It
drives a model through a multi-turn agentic loop, executing tool calls,
respecting permission checks, surfacing approval interrupts to the host
application, and optionally compacting the transcript when it grows too large.
§Architecture
The main entry point is Agent, constructed via AgentBuilder. The
builder optionally accepts the prior conversation transcript via
AgentBuilder::transcript and the next user turn via
AgentBuilder::input — both default to empty. Calling
Agent::start with a SessionConfig returns a LoopDriver that
yields LoopSteps — either a finished turn or an interrupt that
requires host resolution before the loop can continue.
If no input was preloaded, the first call to LoopDriver::next yields
LoopInterrupt::AwaitingInput and the host supplies the first user
turn via InputRequest::submit. If input was preloaded, the first
next() dispatches the model directly — convenient for one-shot calls.
Agent::builder()
.model(adapter) // ModelAdapter implementation
.add_tool_source(registry) // ToolRegistry (or any ToolSource); call again to federate
.permissions(checker) // PermissionChecker for gating tool use
.observer(obs) // LoopObserver for streaming events
.transcript(prior) // optional: passive prior transcript (system prompt, resumed session)
.input(first_user_turn) // optional: preload next user turn so first next() drives a turn
.build()?
.start(config).await? -> LoopDriver
.next().await? -> LoopStep::Finished | LoopStep::Interrupt(...)§Example
use agentkit_core::{Item, ItemKind};
use agentkit_loop::{
Agent, PromptCacheRequest, PromptCacheRetention, SessionConfig,
};
// One-shot: preload system prompt and first user message; first next()
// drives the model directly.
let agent = Agent::builder()
.model(adapter)
.transcript(vec![Item::text(ItemKind::System, "You are a helpful assistant.")])
.input(vec![Item::text(ItemKind::User, "Hello!")])
.build()?;
let mut driver = agent
.start(SessionConfig::new("demo").with_cache(
PromptCacheRequest::automatic().with_retention(PromptCacheRetention::Short),
))
.await?;
let _ = driver.next().await?;Structs§
- Agent
- A configured agent ready to start a session.
- Agent
Builder - Builder for constructing an
Agent. - Input
Request - Descriptor for a
LoopInterrupt::AwaitingInputinterrupt. - LoopCtx
- Read-only context handed to a
LoopMutatoralongside the cursor. - Loop
Driver - The runtime driver that advances the agent loop step by step.
- Loop
Snapshot - A read-only snapshot of the loop driver’s current state.
- Model
Turn Result - Final result produced by a single model turn.
- Pending
Approval - Handle for a pending approval interrupt.
- Prompt
Cache Request - Prompt caching request sent alongside a turn.
- Session
Config - Configuration required to start a new model session.
- Tool
Round Info - Metadata describing a completed tool round, surfaced via
LoopInterrupt::AfterToolResult. - Transcript
Cursor - Mutable handle over the live transcript with dirty tracking.
- Turn
Request - Payload sent to the model at the start of each turn.
- Turn
Result - Outcome of a completed (or cancelled) turn.
Enums§
- Agent
Event - Lifecycle and streaming events emitted by the
LoopDriver. - Loop
Error - Errors that can occur while driving the agent loop.
- Loop
Interrupt - An interrupt that pauses the agent loop until the host resolves it.
- Loop
Step - The result of advancing the agent loop by one step.
- Model
Turn Event - Streaming event emitted by a
ModelTurnduring generation. - Mutation
Point - Where in the loop a
LoopMutatoris given a chance to modify the transcript. Mutators run synchronously at these points; mid-stream mutation (e.g. between content deltas) is intentionally not supported because the assistant item is not yet fully constructed. - Prompt
Cache Breakpoint - Prefix boundary that a provider should cache when using explicit caching.
- Prompt
Cache Mode - Strength of a prompt-cache request.
- Prompt
Cache Retention - High-level provider-neutral cache retention hint.
- Prompt
Cache Strategy - Provider-neutral prompt caching strategy.
Traits§
- Event
Emitter - Sink for emitting
AgentEvents from inside aLoopMutator. The driver supplies a concrete implementation viaLoopCtx::emitter. - Loop
Mutator - Async transcript mutator. Registered via
AgentBuilder::mutatorand invoked at eachMutationPoint. Mutators own their derived state (e.g. running token totals via interior mutability) and decide for themselves whether and how to modify the transcript. - Loop
Observer - Observer hook for streaming agent events to the host application.
- Model
Adapter - Factory for creating model sessions.
- Model
Session - An active model session that can produce sequential turns.
- Model
Turn - A streaming model turn that yields events one at a time.
- Transcript
Observer - Receives full
Items as they are appended to the driver’s transcript.