ExAgent.Jobs (ExAgent v2.3.0)

View Source

Background jobs: tools and sub-agents that keep working while the agent goes on.

A long tool call normally blocks the run until it returns, and is killed by the tool timeout. With ExAgent.Jobs, a call starts a supervised job and returns at once with a job id. The model can keep working, or talking to the user, and later read the result with job_status/wait_job. Inside an ExAgent.Server the finished job can also send its result to the conversation as a new message, which starts a new turn.

Start a job store in your supervision tree:

children = [{ExAgent.Jobs, name: MyApp.Jobs}]

Make any tool, or a whole agent, run in the background:

research = ExAgent.Jobs.agent_tool(
  jobs: MyApp.Jobs,
  name: "research",
  description: "Research a topic in depth. Takes minutes.",
  agent: research_agent,
  notify: fn job, ctx -> ExAgent.Jobs.notify_server(ctx.deps.server, job) end
)

crawl = ExAgent.Jobs.background(crawl_tool, jobs: MyApp.Jobs)

ExAgent.new(model: model, tools: [research, crawl | ExAgent.Jobs.tools(MyApp.Jobs)])

tools/2 returns job_status, wait_job and cancel_job for the model.

Boundaries

Jobs are volatile, like the Server's queue: they live in this process tree and are lost on restart; a lost job is reported as unknown, and the job tools report an unreachable store to the model instead of failing the run. Starting a job is the tool call's outcome, so permissions, approvals and persisted continuations see the start, not the job's later effects. A job runs outside the run that started it: run deadlines, usage limits and cancellation do not apply to it, and its token usage is not added to that run. The tool receives a copy of the run context; run-scoped handles in it (such as execution_scope for ExAgent.run_child/4) are closed once the starting run ends, so background tools must not depend on them.

Options for start_link/1:

  • :max_running — concurrent jobs (default 8); more starts are refused and the model is told to wait.
  • :max_finished — finished jobs kept for status queries (default 256, oldest dropped first).
  • :max_result_bytes — result text kept per job (default 65_536, minimum 256); longer results keep their beginning and end, as ExAgent.Tool.OutputLimit does. Results are converted to text inside the job process, so a result that cannot be encoded never reaches the store.
  • :timeout — default job timeout in milliseconds (default 900_000).

Telemetry: [:exagent, :job, :start] (%{system_time}) and [:exagent, :job, :stop] (%{duration}), with metadata %{job_id, name} and, on stop, status.

Summary

Functions

A tool that runs agent in the background on the prompt the model gives it.

Turn a tool into one that runs in the background.

Stop a running job. A finished job is returned unchanged.

Returns a specification to start this module under a supervisor.

Running and retained finished jobs, newest first.

The message text notify_server/2 sends for a finished job.

Deliver a finished job to a Server conversation as a new message, which the Server runs as a new turn (queued if a run is active). Delivery is best effort: a stopped Server or full queue drops the message; job_status still works.

Run fun as a background job named name. Options: :timeout (ms) and :notify, a one-argument function called with the finished Job. It is not called after cancel/2, nor when a wait/3 caller that is still alive, such as the wait_job tool, received the result as the job finished. Returns {:error, :busy} when :max_running jobs are running.

Start a job store. See the module documentation for options.

Current state of a job.

Tools that let the model follow its jobs: job_status, wait_job and cancel_job. Option :max_wait caps one wait_job call, in seconds (default 60); that tool's timeout is set accordingly.

Wait up to timeout ms for a job to finish; returns its state either way.

Types

notify()

@type notify() :: (ExAgent.Jobs.Job.t() -> term()) | nil

server()

@type server() :: GenServer.server()

Functions

agent_tool(opts)

@spec agent_tool(keyword()) :: ExAgent.Tool.t()

A tool that runs agent in the background on the prompt the model gives it.

Options: :jobs, :name, :description and :agent (required); :run_opts, a keyword list or fn ctx -> keyword end passed to ExAgent.run/3; :timeout; and :notify, as in background/2. The job result is the agent's output. The sub-agent's usage is its own, not the parent run's.

background(tool, opts)

@spec background(ExAgent.Tool.t(), keyword()) :: ExAgent.Tool.t()

Turn a tool into one that runs in the background.

The returned tool keeps the name and argument schema; its description says that it returns a job id. Options: :jobs (required), :timeout (defaults to the tool's own timeout, then to the store's) and :notify, a fn job, ctx -> ... end called when the job finishes (or times out) with the run context of the call that started it, minus its message history. Use notify_server/2 there to continue a Server conversation. Exclusive tools are rejected: jobs run concurrently, so their effects could not be serialized.

cancel(jobs, id)

@spec cancel(server(), String.t()) ::
  {:ok, ExAgent.Jobs.Job.t()} | {:error, :not_found}

Stop a running job. A finished job is returned unchanged.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

list(jobs)

@spec list(server()) :: [ExAgent.Jobs.Job.t()]

Running and retained finished jobs, newest first.

notification(job)

@spec notification(ExAgent.Jobs.Job.t()) :: String.t()

The message text notify_server/2 sends for a finished job.

The Server receives it as a user message. The job output is enclosed in <job_output> tags and marked as data, and closing tags inside it are escaped, but the model may still follow instructions planted in it: treat job output like any other tool result from an untrusted source.

notify_server(server, job)

@spec notify_server(GenServer.server(), ExAgent.Jobs.Job.t()) ::
  :ok | {:error, term()}

Deliver a finished job to a Server conversation as a new message, which the Server runs as a new turn (queued if a run is active). Delivery is best effort: a stopped Server or full queue drops the message; job_status still works.

start(jobs, name, fun, opts \\ [])

@spec start(server(), String.t(), (-> term()), keyword()) ::
  {:ok, ExAgent.Jobs.Job.t()} | {:error, :busy}

Run fun as a background job named name. Options: :timeout (ms) and :notify, a one-argument function called with the finished Job. It is not called after cancel/2, nor when a wait/3 caller that is still alive, such as the wait_job tool, received the result as the job finished. Returns {:error, :busy} when :max_running jobs are running.

start_link(opts \\ [])

Start a job store. See the module documentation for options.

status(jobs, id)

@spec status(server(), String.t()) ::
  {:ok, ExAgent.Jobs.Job.t()} | {:error, :not_found}

Current state of a job.

tools(jobs, opts \\ [])

@spec tools(server(), keyword()) :: [ExAgent.Tool.t()]

Tools that let the model follow its jobs: job_status, wait_job and cancel_job. Option :max_wait caps one wait_job call, in seconds (default 60); that tool's timeout is set accordingly.

wait(jobs, id, timeout)

@spec wait(server(), String.t(), non_neg_integer()) ::
  {:ok, ExAgent.Jobs.Job.t()} | {:error, :not_found}

Wait up to timeout ms for a job to finish; returns its state either way.