Skip to content

Setting ToolChoice to 'none' in lifecycle hook has no effect #116

Open
@desmondpp

Description

@desmondpp

Describe the bug

As far as I can tell there's no way to give the agent the flexibility to call a tool as often as necessary to answer a user prompt, but cap it to prevent runaway calls. While this can be achieved through prompt engineering, I had mixed results so hoped to have a mechanism in the agent SDK. I attempted to do so by adding a lifecycle hook and tracking the tool calls. When the calls are > N, I set toolChoice to none. However despite this the tool continued to be called.

Debug information

  • Agents SDK version: (e.g. v0.0.7)
  • Runtime environment (e.g. Node.js 22.16.0)

Repro steps

Include a system prompt telling the agent to call the tool at least 3 times (to trigger the code below).

export function createKnowledgeCenterAgent(model: string) {
  const searchTool = createKnowledgeCenterSearchTool();

  const agent = new Agent<KnowledgeCenterAgentContext, typeof KnowledgeCenterAgentOutputSchema>({
    name: 'Knowledge Center Agent',
    instructions: KNOWLEDGE_CENTER_PROMPT,
    model,
    tools: [searchTool],
    outputType: KnowledgeCenterAgentOutputSchema,
  });
  let toolCallCount = 0;
  agent.on('agent_tool_end', () => {
    if (++toolCallCount >= 2) {
      agent.modelSettings.toolChoice = 'none';
    }
  });

  return agent;
}

Expected behavior

I expected the tool calls to discontinue once toolChoice was set to none.

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