Human approval
Some tool calls should not run until a person says so: sending an email, spending money, deleting a record. Freyja lets the guard answer Decision::Ask, which pauses the run with the calls stored, so the decision can come later, from another request, or after the process restarts.
Pausing
A guard returns Allow, Deny(reason) or Ask for each call the model requests. When any call of a turn gets Ask, the run ends with StopReason::Paused before any tool of that turn runs, including the calls the guard allowed. The run is Ok, since nothing failed.
use freyja::{Agent, Decision, JsonlStorage, StopReason};
let agent = Agent::new(client)
.tool(send_email)
.guard(|call, _cx| match call.name.as_str() {
"send_email" => Decision::Ask,
_ => Decision::Allow,
});
let mut chat = agent.conversation(JsonlStorage::open("chat.jsonl")?);
let run = chat.send("Email the report to Sam").await?;
This example needs a key and network, because it calls a real model.
Reading what is waiting
Conversation::pending returns the tool calls waiting for an answer, as ToolCall values with id, name and raw JSON arguments. It reads them from storage, so it works in a new process that reopens the same file or database. It returns an empty list when nothing waits.
if run.stop == StopReason::Paused {
for call in chat.pending().await? {
println!("{} wants to run with {}", call.name, call.arguments);
}
}
A run that a hook or the token budget stopped before its tools ran leaves its calls waiting too, so pending lists those as well.
Resuming
Conversation::resume takes a closure that returns a Decision for each waiting call and carries the conversation on.
let run = chat.resume(|_call| Decision::Allow).await?;
println!("{}", run.answer);
Allow runs the call. Deny(reason) tells the model why not, in the same denied: <reason> form a guard denial uses. To decide call by call, match on call.id or call.name.
resume takes .context(&cx) and .stream(on_event) as send does.
Each resume is a new run, with its own turn limit and its own token budget. It fails when no call is waiting.
The guard still applies
resume asks the closure only about the waiting calls the guard does not deny. If the guard now denies a call, the model reads the denial and the closure is never consulted for it. A person's approval cannot override the guard.
Answering Ask again
If the closure returns Decision::Ask for any call, nothing runs and the run pauses again with the same calls waiting. That is how a caller says "not yet" without deciding.
Results are saved before the next model call
The tool results are written to storage before the model is called again, so a call that ran is not run twice when that next request fails. A process that dies while a tool runs can still run it again on the next resume. A tool with a side effect should be safe to repeat, for example by checking an idempotency key.
Sending instead abandons the calls
Sending a new message with send instead of resuming abandons the waiting calls. They stay in storage unanswered, the next send leaves them out of what reaches the model, and they never run.
Surviving a restart
Waiting calls live in the transcript, so any storage that outlives the process keeps them. JsonlStorage::open(path) is the simplest: a later process opens the same file, builds the same agent, and calls pending and resume. A storage you wrote yourself works the same way. InMemoryStorage loses them with the process.
// A new process, the same file.
let mut chat = agent.conversation(JsonlStorage::open("chat.jsonl")?);
let waiting = chat.pending().await?;
Build the agent with the same tools and guard as before the restart, because resume runs the stored calls on the agent you give it.
A web flow
A common shape is two requests. The first sends the user's message and answers with the calls that need approval. The second carries the approval and returns the final answer. Neither request keeps anything in memory between them, because the conversation is in storage.
use freyja::{Agent, Decision, JsonlStorage, StopReason};
use std::collections::HashSet;
struct Waiting {
id: String,
name: String,
arguments: String,
}
/// Request one: run the message, report what needs approval.
async fn send(agent: &Agent, chat_id: &str, text: &str) -> Result<Vec<Waiting>, Box<dyn std::error::Error>> {
let mut chat = agent.conversation(JsonlStorage::open(format!("chats/{chat_id}.jsonl"))?);
let run = chat.send(text).await?;
if run.stop != StopReason::Paused {
return Ok(Vec::new());
}
Ok(chat
.pending()
.await?
.into_iter()
.map(|call| Waiting { id: call.id, name: call.name, arguments: call.arguments })
.collect())
}
/// Request two: the person approved some call ids.
async fn approve(agent: &Agent, chat_id: &str, approved: HashSet<String>) -> Result<String, Box<dyn std::error::Error>> {
let mut chat = agent.conversation(JsonlStorage::open(format!("chats/{chat_id}.jsonl"))?);
let run = chat
.resume(|call| {
if approved.contains(&call.id) {
Decision::Allow
} else {
Decision::Deny("the user did not approve this".to_string())
}
})
.await?;
Ok(run.answer)
}
A real handler also checks that the person approving owns the conversation and that chat_id cannot reach another user's file. The Waiting values are what your page shows. A run that pauses again after resume, because the new turn asked for another approval, returns StopReason::Paused, so a production handler checks run.stop there as well.
Limits
Agent::ask keeps no history, so a paused ask cannot be resumed. Use a conversation when a guard can ask.
A sub-agent that pauses does not pause its caller. The calling model reads an error: result, as for any inner run that does not answer. See Multi-agent.
See also
- Conversations for sending, streaming and the rest of a transcript
- Agents for the guard and the builder
- Hooks for ending a run without a person, and for auditing refused calls
- Multi-agent for how a guard sees a team's transfer calls
- Refusing a call for the guard
- Storage for
JsonlStorageand writing your own backend