Watchfire
LLM helpers
LLM helpers (the llm feature: smeltery = { …, features = ["llm"] }). smeltery::watchfire::llm has the
Provider trait (complete(Request) -> Response with the system prompt, messages, tool definitions, the answer's
content blocks, stop reason and input / output token usage), FakeProvider (scripted replies and tool calls, every
request recorded), Anthropic (the Messages API through ctx.http(), the key from ANTHROPIC_API_KEY, the model and
max_tokens set in AnthropicConfig; its calls follow no redirect, so the key and the prompt go only to the base URL,
and its Debug output hides the key) and Agent, a tool-calling loop:
use smeltery::watchfire::llm::{Agent, Anthropic, AnthropicConfig, Budget, Price};
#[derive(serde::Deserialize)]
struct Lookup { symbol: String }
let claude = Anthropic::from_env(ctx.http().clone(), AnthropicConfig::new("claude-opus-5-5", 16_000))?;
let outcome = Agent::new(claude)
.system("You answer questions about stock prices.")
.tool("price", "The latest price of a stock symbol",
serde_json::json!({"type": "object", "properties": {"symbol": {"type": "string"}}, "required": ["symbol"]}),
|input: Lookup| async move { Ok(format!("{}: 42.00", input.symbol)) })
.max_turns(8)
.budget(Budget::new().max_tokens(200_000).max_cost(1.0).price("claude-opus-5-5", Price::per_mtok(4.0, 20.0)))
.cancel_on(ctx.token().clone())
.run("What does ACME trade at?")
.await?;
ctx.log().info(outcome.text);
Tool calls of one answer run in order and their results go back in one message; a tool error, an input that does
not match the tool's type, or an unknown tool goes back to the model as an error result. The loop stops with
MaxTurns after max_turns model calls (default 10) and with BudgetExceeded once the tokens or the cost (from the
prices you give per model) pass the budget. Rate limits (429), overload (529), 5xx answers and network errors are
retried (default 3 times, backoff 1 s..=30 s with jitter, honouring retry-after); other errors are not.