Skip to content

Enable to customize the internal runner for an agent as tool #460

@alonhar

Description

@alonhar

Currently the asTool() helper on Agent only supports toolName, toolDescription, and customOutputExtractor.
However, the underlying runner.run() already supports a maxTurn parameter, which controls the maximum number of turns for the run.

We need to allow users of asTool() to configure this value directly.

Current code (simplified)

asTool(options: {
  toolName?: string;
  toolDescription?: string;
  customOutputExtractor?: (
    output: RunResult<TContext, Agent<TContext, any>>,
  ) => string | Promise<string>;
}): FunctionTool {
  const { toolName, toolDescription, customOutputExtractor } = options;
  return tool({
    name: toolName ?? toFunctionToolName(this.name),
    description: toolDescription ?? '',
    ...
    execute: async (data, context) => {
      const runner = new Runner();
      const result = await runner.run(this, data.input, {
        context: context?.context,
      });
      ...
    },
  });
}

Requested change

Add a new maxTurn?: number option to the asTool() options.

Pass it through to runner.run().

Proposed signature:

asTool(options: {
  toolName?: string;
  toolDescription?: string;
  maxTurn?: number;
  customOutputExtractor?: (
    output: RunResult<TContext, Agent<TContext, any>>,
  ) => string | Promise<string>;
}): FunctionTool


Update the runner.run call:

const result = await runner.run(this, data.input, {
  context: context?.context,
  maxTurn,
});

This ensures that agents wrapped as tools can fully leverage the maxTurn functionality already available in runner.run(), enabling better control over multi-turn execution directly from the tool API.

What do you think? (I can open PR)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions