glean/run
Agent execution loop.
A pure recursive loop: call the model, run any tools it asked for, feed
the results back, and repeat until the model answers without calling a
tool (or max_steps is reached).
Every tool call runs in a process of its own, so a tool that panics, or
takes longer than agent.tool_timeout allows, can’t take the run down
with it: the model gets an error result and can try something else.
When the process running the agent is itself stopped, its tools are
stopped with it.
The tool therefore does not run in the process that called run:
process.self() is another process there, and a tool can’t receive on
a subject the caller made. Give tools what they need through deps.
Types
The result of a completed agent run.
pub type RunResult {
RunResult(
text: String,
messages: List(message.Message),
content: List(provider.GeneratedContent),
usage: usage.Usage,
finish_reason: error.FinishReason,
steps: Int,
step_results: List(StepResult),
)
}
Constructors
-
RunResult( text: String, messages: List(message.Message), content: List(provider.GeneratedContent), usage: usage.Usage, finish_reason: error.FinishReason, steps: Int, step_results: List(StepResult), )Arguments
- text
-
The final text output (concatenated from all text content)
- messages
-
The whole conversation: input messages, every tool call and result, and the model’s final answer. Pass the result to
continueto keep talking. - content
-
All generated content from the final step
- usage
-
Total token usage across all steps
- finish_reason
-
Why the model stopped
- steps
-
Number of steps taken
- step_results
-
Results from each individual step
The result of a single step in the agent loop.
pub type StepResult {
StepResult(
step: Int,
content: List(provider.GeneratedContent),
finish_reason: error.FinishReason,
usage: usage.Usage,
)
}
Constructors
-
StepResult( step: Int, content: List(provider.GeneratedContent), finish_reason: error.FinishReason, usage: usage.Usage, )
Values
pub fn continue(
agent: agent.Agent(deps),
deps: deps,
previous: RunResult,
prompt: String,
) -> Result(RunResult, error.GleanError)
Continue a previous run with additional user input.
pub fn continue_stream(
agent: agent.Agent(deps),
deps: deps,
previous: RunResult,
prompt: String,
on_event: fn(stream.StreamEvent) -> Nil,
) -> Result(RunResult, error.GleanError)
Continue a previous run with additional user input, streaming the
response. The streaming version of continue.
pub fn generate(
agent: agent.Agent(deps),
deps: deps,
prompt: String,
) -> Result(RunResult, error.GleanError)
Run the agent with a simple text prompt.
pub fn generate_messages(
agent: agent.Agent(deps),
deps: deps,
messages: List(message.Message),
) -> Result(RunResult, error.GleanError)
Run the agent with pre-built messages (for multi-turn conversations).
The agent’s system prompts are prepended unless messages already
contains a system message, so a history taken from RunResult.messages
doesn’t get them twice.
pub fn stream(
agent: agent.Agent(deps),
deps: deps,
prompt: String,
on_event: fn(stream.StreamEvent) -> Nil,
) -> Result(RunResult, error.GleanError)
Run the agent with streaming. Calls on_event for each StreamEvent.
pub fn stream_messages(
agent: agent.Agent(deps),
deps: deps,
messages: List(message.Message),
on_event: fn(stream.StreamEvent) -> Nil,
) -> Result(RunResult, error.GleanError)
Run the agent with streaming and pre-built messages (for multi-turn
conversations). The streaming version of generate_messages.
The agent’s system prompts are prepended unless messages already
contains a system message, so a history taken from RunResult.messages
doesn’t get them twice.