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).

Types

The result of a completed agent run.

pub type RunResult {
  RunResult(
    text: String,
    messages: List(message.Message),
    content: List(provider.GeneratedContent),
    usage: stream.Usage,
    finish_reason: error.FinishReason,
    steps: Int,
    step_results: List(StepResult),
  )
}

Constructors

  • RunResult(
      text: String,
      messages: List(message.Message),
      content: List(provider.GeneratedContent),
      usage: stream.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 continue to 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: stream.Usage,
  )
}

Constructors

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.

✨ Search Document