pub struct Agent { /* private fields */ }Expand description
Builds one agent from a name to an answer: model, instructions, tools,
evals, limits, storage and laws. Setters never fail; build checks
everything, reports the first problem and makes no model call.
run and run_with build and run in one call.
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Starts a builder for the agent called name.
The name must be 1 to 64 of [a-z0-9-]; build checks it.
Every other setting starts at its documented default.
Sourcepub fn model(self, selector: impl Into<String>) -> Self
pub fn model(self, selector: impl Into<String>) -> Self
Sets the model: vendor/model from the catalog, or an alias that
needs .model_client(..). Required.
Returns the builder.
Sourcepub fn instructions(self, text: impl Into<String>) -> Self
pub fn instructions(self, text: impl Into<String>) -> Self
Sets the system instructions. Required; the macro uses the doc comment. The text is kept exactly, never parsed as TOML. Returns the builder.
Sourcepub fn display_name(self, text: impl Into<String>) -> Self
pub fn display_name(self, text: impl Into<String>) -> Self
Sets the display name shown in the system prompt. Defaults to the agent name. Returns the builder.
Sourcepub fn role(self, text: impl Into<String>) -> Self
pub fn role(self, text: impl Into<String>) -> Self
Sets the role line of the system prompt.
Defaults to "An assistant.".
Returns the builder.
Sourcepub fn goal(self, text: impl Into<String>) -> Self
pub fn goal(self, text: impl Into<String>) -> Self
Sets the goal line of the system prompt.
Defaults to "Answer the user's request.".
Returns the builder.
Sourcepub fn tool(self, tool: impl AsTool) -> Self
pub fn tool(self, tool: impl AsTool) -> Self
Adds a tool: a #[tool] fn or a tool(..) value.
A second tool under a taken name fails at build.
Returns the builder.
Sourcepub fn tool_arc(self, tool: Arc<dyn Tool>) -> Self
pub fn tool_arc(self, tool: Arc<dyn Tool>) -> Self
Adds a tool already behind an Arc<dyn Tool>.
Same checks as tool at build.
Returns the builder.
Sourcepub fn tools(self, tools: impl IntoIterator<Item = Arc<dyn Tool>>) -> Self
pub fn tools(self, tools: impl IntoIterator<Item = Arc<dyn Tool>>) -> Self
Adds every tool of tools, in order.
Same checks as tool at build.
Returns the builder.
Sourcepub fn toolkit(self, toolkit: Toolkit) -> Self
pub fn toolkit(self, toolkit: Toolkit) -> Self
Adds every tool of toolkit, in order.
Same checks as tool at build.
Returns the builder.
Sourcepub fn eval(self, call: impl Into<String>) -> Self
pub fn eval(self, call: impl Into<String>) -> Self
Adds one persona eval call, such as min_items(items, 2).
Parsed and checked against registered evals at build.
Returns the builder.
Sourcepub fn evals<S: Into<String>>(self, calls: impl IntoIterator<Item = S>) -> Self
pub fn evals<S: Into<String>>(self, calls: impl IntoIterator<Item = S>) -> Self
Adds every eval call of calls, in order.
Same checks as eval at build.
Returns the builder.
Sourcepub fn eval_fn(self, name: impl Into<String>, f: EvalFn) -> Self
pub fn eval_fn(self, name: impl Into<String>, f: EvalFn) -> Self
Registers custom eval name running f, next to the built-ins.
An invalid, reserved or taken name fails at build.
Returns the builder.
Sourcepub fn memory(self, memory: AgentMemory) -> Self
pub fn memory(self, memory: AgentMemory) -> Self
Gives the agent long-term memory for one user and session: the five
memory tools, bound to them, and a preamble of facts and summary.
Needs AgentMemory::sqlite(..) or .knowledge(..) in 0.2.
Sourcepub fn knowledge(self, knowledge: impl Into<Knowledge>) -> Self
pub fn knowledge(self, knowledge: impl Into<Knowledge>) -> Self
Lets the agent search a folder: ingested at build (unchanged files
skipped) and served by the search_knowledge tool, fenced as
untrusted text. Takes Knowledge::dir(..) or a path.
Sourcepub fn limits(self, limits: Limits) -> Self
pub fn limits(self, limits: Limits) -> Self
Sets the spend limits checked before every model call.
Defaults to Limits::default(), no limit.
Returns the builder.
Sourcepub fn max_turns(self, turns: u32) -> Self
pub fn max_turns(self, turns: u32) -> Self
Caps model turns per run; 2 to 50, checked at build.
Defaults to DEFAULT_MAX_TURNS.
Returns the builder.
Sourcepub fn max_tokens(self, tokens: u32) -> Self
pub fn max_tokens(self, tokens: u32) -> Self
Caps output tokens per model call; 1 to 200000, checked at build.
Defaults to DEFAULT_MAX_TOKENS.
Returns the builder.
Sourcepub fn checkpoints(self, storage: Storage) -> Self
pub fn checkpoints(self, storage: Storage) -> Self
Sets where run checkpoints live.
Defaults to Storage::memory(), fresh per run.
Returns the builder.
Sourcepub fn catalog(self, catalog: Catalog) -> Self
pub fn catalog(self, catalog: Catalog) -> Self
Replaces the built-in provider catalog.
Model selectors and prices resolve against it at build.
Returns the builder.
Sourcepub fn api_key(self, key: Secret) -> Self
pub fn api_key(self, key: Secret) -> Self
Sets the API key, beating any environment variable.
The key never appears in Debug or an error.
Returns the builder.
Sourcepub fn api_key_env(self, name: impl Into<String>) -> Self
pub fn api_key_env(self, name: impl Into<String>) -> Self
Reads the API key from environment variable name at build.
Defaults to <VENDOR>_API_KEY.
Returns the builder.
Sourcepub fn transport(self, transport: Arc<dyn Transport>) -> Self
pub fn transport(self, transport: Arc<dyn Transport>) -> Self
Sets the HTTP transport for catalog models.
Defaults to a new reqwest transport (feature reqwest).
Returns the builder.
Sourcepub fn model_client(self, model: Arc<dyn Model>) -> Self
pub fn model_client(self, model: Arc<dyn Model>) -> Self
Serves model for the selector instead of connecting one.
For tests, aliases and custom hosts; needs no key or network.
Returns the builder.
Sourcepub fn price(self, price: Price) -> Self
pub fn price(self, price: Price) -> Self
Sets the price replies are charged at. Defaults to the catalog price, else free with a model client. Returns the builder.
Sourcepub fn events(self, sink: Arc<dyn EventSink>) -> Self
pub fn events(self, sink: Arc<dyn EventSink>) -> Self
Sends every run event to sink.
Defaults to a sink that drops events, so memory stays bounded.
Returns the builder.
Sourcepub fn ledger(self, ledger: Arc<dyn UsageLedger>) -> Self
pub fn ledger(self, ledger: Arc<dyn UsageLedger>) -> Self
Records usage in ledger for period limits and a route’s daily
cloud spend. Default: in memory per built agent, lost on restart;
pass one durable ledger to agents that share a route’s cap.
Sourcepub fn retries(self, retries: u32) -> Self
pub fn retries(self, retries: u32) -> Self
Sets how often a transient provider failure is retried per run.
At most 32, checked at build; defaults to 3.
Returns the builder.
Sourcepub fn laws(self, path: impl AsRef<Path>) -> Self
pub fn laws(self, path: impl AsRef<Path>) -> Self
Adds a .bend law file, checked at build.
0.2 stores laws; no run proves them yet.
Returns the builder.
Sourcepub fn law(self, law: impl Into<String>) -> Self
pub fn law(self, law: impl Into<String>) -> Self
Adds one inline law, checked at build.
0.2 stores laws; no run proves them yet.
Returns the builder.
Sourcepub fn auto_laws(self, on: bool) -> Self
pub fn auto_laws(self, on: bool) -> Self
Asks for laws derived from the code as well. Defaults to true.
0.2 records the choice only.
Returns the builder.
Sourcepub async fn build(self) -> Result<BuiltAgent, KitError>
pub async fn build(self) -> Result<BuiltAgent, KitError>
Checks every setting and returns a ready, shareable agent. Fails with the first problem: settings, tools, persona, laws, evals, model, key or storage. Makes no model call.
Source§impl Agent
impl Agent
Sourcepub fn route(self, policy: RoutePolicy) -> Self
pub fn route(self, policy: RoutePolicy) -> Self
Routes each model call by policy: target cloud is the
.model(..) selector, target local the .local(..) model; any
other target id fails build. Replaces route_file.
Sourcepub fn route_file(self, path: impl AsRef<Path>) -> Self
pub fn route_file(self, path: impl AsRef<Path>) -> Self
Routes by the policy in TOML file path, read and checked at
build (at most 64 KiB). Same targets as route.
Replaces route.
Sourcepub fn private(self, on: bool) -> Self
pub fn private(self, on: bool) -> Self
Keeps the agent’s model requests on the device, even on fallback.
Default false. build refuses a private agent with no on-device
model, a cloud knowledge embedder, or a judge eval naming a cloud
model. Tools are not covered: web_fetch, web_search and HTTP
tools still send their requests over the network.
Sourcepub fn tier(self, name: impl Into<String>) -> Self
pub fn tier(self, name: impl Into<String>) -> Self
Sends requests only to route targets that list task tier name.
Needs .route(..); a tier no target serves fails build.
Default: no tier.
Sourcepub fn network(self, state: NetworkState) -> Self
pub fn network(self, state: NetworkState) -> Self
Reads the network state for routing from state, which the app
keeps a clone of and sets. Without it a passive probe infers the
state from network errors.
Sourcepub fn device(self, state: DeviceState) -> Self
pub fn device(self, state: DeviceState) -> Self
Reads battery and thermal state for routing from state, which the
app keeps a clone of and sets. Without it both are unknown and the
policy’s device rules are skipped.
Source§impl Agent
impl Agent
Sourcepub fn tracing(self, on: bool) -> Self
pub fn tracing(self, on: bool) -> Self
Turns tracing on with every category, or off. Default off.
Covers build and every run; spans go to trace_to, trace_file
or ./traces/<name>-<trace id>.jsonl ($TROUPEKIT_TRACE_DIR).
Sourcepub fn trace(self, categories: impl IntoIterator<Item = Trace>) -> Self
pub fn trace(self, categories: impl IntoIterator<Item = Trace>) -> Self
Turns tracing on for the listed categories only. An empty list turns tracing off. Returns the builder.
Sourcepub fn trace_content(self, on: bool) -> Self
pub fn trace_content(self, on: bool) -> Self
Keeps prompt, reply, tool argument, query and document text in spans. Default off: spans then hold only byte counts. Secrets are scrubbed either way.
Sourcepub fn trace_to(self, sink: Arc<dyn TraceSink>) -> Self
pub fn trace_to(self, sink: Arc<dyn TraceSink>) -> Self
Sends trace records to sink instead of a file.
Takes effect only with tracing on; replaces trace_file.
Returns the builder.
Sourcepub fn trace_file(self, path: impl AsRef<Path>) -> Self
pub fn trace_file(self, path: impl AsRef<Path>) -> Self
Writes trace records to the new file path; an existing file is
refused at build. Takes effect only with tracing on; replaces
trace_to.