agentkit-core
Shared primitives for agentkit transcripts, content parts, usage accounting, identifiers, and cancellation.
This crate defines the data model used across the rest of the workspace:
- transcript items and roles
- multimodal content parts
- tool call and tool result payloads
- streaming deltas
- token and cost usage
- cancellation checkpoints for turns and tools
Most other crates in the workspace depend on agentkit-core as their common language for messages and events.
Building a transcript for the agent loop
Every agent turn starts with a Vec<Item> transcript. System instructions,
user messages, and context documents are all items; the loop appends assistant
and tool items as the turn progresses.
use ;
let transcript = vec!;
assert_eq!;
assert_eq!;
Representing tool calls and results
When the model invokes a tool the loop emits a ToolCallPart. After execution
the tool executor wraps the output in a ToolResultPart and appends it back
to the transcript as a Tool item so the model can observe the result.
use ;
use json;
// The model asks to read a file.
let tool_call = new;
// After execution, the tool executor produces a result item.
let tool_result_item = new;
assert!;
Tracking token usage across turns
Usage and TokenUsage let you accumulate costs and token counts reported by
model providers. Reporters and compaction triggers inspect these values to
decide when to summarize or stop.
use ;
let turn_usage = new
.with_cost;
let tokens = turn_usage.tokens.as_ref.unwrap;
assert_eq!;
Cancelling a running turn
Create a CancellationController and hand its handle() to whatever drives
the model — typically AgentBuilder::cancellation in agentkit-loop. Tools
and adapters take a TurnCancellation checkpoint and observe its
is_cancelled() flag (or cancelled() future) to bail out cooperatively.
When the loop returns, a turn that was interrupted carries
FinishReason::Cancelled.
use ;
let controller = new;
let handle = controller.handle;
// Hand `handle.clone()` to the agent loop, model adapter, or tool executor.
// Inside a turn, the consumer captures a checkpoint:
let checkpoint = new;
assert!;
// A Ctrl-C listener (or any other interrupt source) fires `interrupt()`,
// flipping every outstanding checkpoint to cancelled.
controller.interrupt;
assert!;