glean/agent

Types

An agent definition. Generic over deps — the dependency type threaded through to tools during execution.

pub type Agent(deps) {
  Agent(
    provider: provider.Provider,
    system_prompts: List(String),
    tools: tool.Toolset(deps),
    settings: provider.ModelSettings,
    tool_choice: provider.ToolChoice,
    max_steps: Int,
    response_format: provider.ResponseFormat,
    parallel_tool_calls: Bool,
    tool_timeout_ms: option.Option(Int),
    output_retries: Int,
  )
}

Constructors

  • Agent(
      provider: provider.Provider,
      system_prompts: List(String),
      tools: tool.Toolset(deps),
      settings: provider.ModelSettings,
      tool_choice: provider.ToolChoice,
      max_steps: Int,
      response_format: provider.ResponseFormat,
      parallel_tool_calls: Bool,
      tool_timeout_ms: option.Option(Int),
      output_retries: Int,
    )

    Arguments

    provider

    The provider to use for generation

    system_prompts

    System prompt(s) prepended to every conversation

    tools

    Available tools

    settings

    Default model settings

    tool_choice

    Tool choice strategy

    max_steps

    Maximum number of generate-tool-loop steps (safety limit)

    response_format

    Plain text, or JSON (optionally constrained by a schema). With a schema, the final answer is validated against it.

    parallel_tool_calls

    Run several tool calls from one model response concurrently (default: True). Results are always returned in the model’s order.

    tool_timeout_ms

    The longest one tool call may take, retries included, in milliseconds (default: no limit). A call that takes longer is stopped and reported to the model as an error.

    output_retries

    How many times the model is asked again when its final answer doesn’t match the JSON schema (default: 1).

Values

pub fn from(model: model.Model) -> Agent(deps)

Create an agent from a type-safe Model. The Model carries both the provider and validated settings.

pub fn json_output(
  agent: Agent(deps),
  schema schema: option.Option(schema.Schema),
  name name: option.Option(String),
) -> Agent(deps)

Ask for JSON output. With a schema, the provider is asked to follow it (where the API supports schemas) and the final answer is validated against it. On a mismatch the model is told what is wrong and asked again, as often as output_retries allows (once by default); after that the run fails with OutputValidationError, which carries the answer, the conversation and the tokens used.

agent.from(model)
|> agent.json_output(schema: Some(person_schema), name: Some("person"))
pub fn max_steps(agent: Agent(deps), max: Int) -> Agent(deps)

Set the maximum number of agent loop steps.

pub fn new(provider: provider.Provider) -> Agent(deps)

Create a new agent with the given provider. All other fields get sensible defaults.

pub fn output_retries(
  agent: Agent(deps),
  retries: Int,
) -> Agent(deps)

Set how many times the model is asked again when its final answer doesn’t match the schema given to json_output (default: 1; 0 fails the run on the first mismatch). Each retry is one more step, so it also counts towards max_steps. An answer cut off at the output token limit is never retried: it would be cut off again.

pub fn sequential_tool_calls(agent: Agent(deps)) -> Agent(deps)

Run tool calls one after another instead of concurrently. Use this when your tools must not run at the same time (e.g. they share a resource).

pub fn settings(
  agent: Agent(deps),
  settings: provider.ModelSettings,
) -> Agent(deps)

Set model settings.

pub fn system(agent: Agent(deps), prompt: String) -> Agent(deps)

Add a system prompt to the agent.

pub fn tool_choice(
  agent: Agent(deps),
  choice: provider.ToolChoice,
) -> Agent(deps)

Set the tool choice strategy.

pub fn tool_timeout(agent: Agent(deps), ms: Int) -> Agent(deps)

Limit how long one tool call may take, retries included. A call that takes longer is stopped, and the model gets an error result saying so, like any other tool failure. Without a limit, a tool that never returns keeps the run waiting forever.

pub fn tools(
  agent: Agent(deps),
  tools: tool.Toolset(deps),
) -> Agent(deps)

Set the tools available to this agent.

✨ Search Document