Skip to contents

Turns an Agent into an LLMR::llm_tool(), so other agents can delegate to it. A supervisor with specialist sub-agents in its tools decides for itself when to consult whom; the specialist's reply comes back as the tool result, and the supervisor continues with it.

Usage

agent_as_tool(x, name = NULL, description = NULL)

Arguments

x

The Agent to expose.

name

Tool name shown to the calling model. Default ask_<name>, lower-cased and sanitized.

description

What the calling model is told about this specialist. Defaults to the first sentence of the persona, prefixed by the agent's name. Write this the way you would brief a colleague: what the specialist knows and when to consult it.

Value

An LLMR::llm_tool() object, ready for agent(tools = ...).

Details

Three properties make delegation safe and auditable:

  • Spend is attributed. A consultation runs through the specialist's own machinery, so its usage() and trace() record the work it did, while the supervisor's trace records the tool call.

  • Budgets nest. Give the specialist its own budget(); when it is exhausted, the supervisor receives the budget error as the tool result (an "ERROR: ..." string) and can carry on without it.

  • Consultations are stateless. Each delegated question goes through reply(): the specialist's persona applies, but nothing is written to its memory, so concurrent supervisors cannot contaminate each other.

Examples

if (FALSE) { # \dontrun{
cfg <- LLMR::llm_config("groq", "openai/gpt-oss-20b", temperature = 0.3)

statistician <- agent("Stat", cfg,
  persona = "A PhD statistician. Precise about assumptions and pitfalls.",
  budget  = budget(max_calls = 5))   # the specialist has its own ceiling
historian <- agent("Hist", cfg,
  persona = "An economic historian. Strong on institutional context.")

supervisor <- agent("Lead", cfg,
  persona = "A research lead. Consult your specialists, then synthesize.",
  tools = list(agent_as_tool(statistician), agent_as_tool(historian)))

supervisor$chat(
  "We observe falling crime and rising policing budgets across cities.
   What would it take to argue causality here?")
statistician$usage()   # the consultation is on the specialist's meter
} # }