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
Wire a CancellationController into the agent and spawn a Ctrl-C listener
that fires interrupt(). The loop checks the handle between steps and
returns FinishReason::Cancelled when triggered.
use CancellationController;
use ;
use FinishReason;
use ;
# async