sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Lazy Loading for Agent.as_tool() to reduce cold-start latency

Open jakeogrady opened this issue 1 month ago • 2 comments

Human Overview

I would like the ability to construct AgentasTools lazily, rather than eagerly building every AgentasTools for every request, even if the turn doesn't use that agent. As we grow the number of sub-agents under our orchestrator, this makes the build time for these AgentasTools slower and waste time on Agents that won't even be needed for the conversation potentially. I think that Agent-as-tools architectures such as ours should allow for Agents to be built when called with stream().

SDK Language

Python

Problem Statement

Currently, Agent.as_tool() eagerly construct all sub-agents during registration, which causes excessive loading and high first call latency for orchestrator agents with many sub-agents.

I would like Strands to support lazy loading of agents as tools. This would defer sub-agent construction until the tool is first invoked. The tool would:

  1. Provide tool metadata (tool_name, tool_spec, tool_type) immediately without building the agent
  2. Only invoke build_fn when stream() is first called
  3. Cache the built agent for reuse on subsequent calls

Proposed Solution

Add an optional lazy=True parameter to Agent.as_tool():

orchestrator = Agent(
   agent_id="orchestrator",
   tools=[
       subagent_a.as_tool(lazy=True),
       subagent_b.as_tool(lazy=True),
       subagent_c.as_tool(lazy=True),
   ],
)

So then:

  • lazy=True defers agent construction until first stream() call
  • Tool metadata is extracted from the agent's properties upfront (no separate schema needed)
  • Construction is thread-safe with double-checked locking for concurrent access
  • Build errors are wrapped in a SubAgentBuildError to avoid echoing model input
  • Set lazy=False as the default so that users can insist that some agents should always be loaded

Use Case

Multi-agent orchestrator with 10+ sub-agents where:

  • Only 2-3 sub-agents are typically called per request
  • Sub-agent initialization is expensive (model loading, API clients, etc.)
  • Orchestrator startup time is a critical bottleneck

For example, if someone has some sub-agents that are not regularly as used, there is no need to always construct them on the first request when they might even be used at all in the conversation.

Alternatives Solutions

No response

Additional Context

No response

jakeogrady avatar Oct 06 '26 15:10 jakeogrady

I'd like to help with this feature. On current main (e519d67), Agent.as_tool() wraps an already-created Agent, so adding lazy=True to that instance method alone cannot defer its construction. Would you accept a factory-based SDK tool with explicit name/description, building the Agent on first invocation and caching it per tool instance?

I also noticed the Harness subagent tool already uses an AgentSpec/builder flow. Should this request extend that existing mechanism, or is a reusable SDK-level adapter still in scope?

If accepted, I'd start with synchronous factories, deterministic tests for zero construction during registration, one successful construction across repeated calls, concurrent initialization and build failures, while preserving the existing agent-as-tool streaming and context semantics. I'd settle the API and ownership before implementation. Is anyone already working on this?

AI assistance: this source review and draft were prepared with an AI coding assistant.

Donyes avatar Oct 08 '26 14:10 Donyes

A brief follow-up: is a factory-based, lazily initialized agent-as-tool adapter still in scope for the SDK, or would you prefer this to build on the Harness subagent mechanism? I'm happy to follow the preferred direction. Thanks!

AI-assisted draft.

Donyes avatar Oct 21 '26 06:10 Donyes