[FEATURE] Support for async Tool
Problem Statement
Hi team,
I’m looking to create tools for LLM calls and search systems, but these functions are implemented as async functions in my codebase.
Does StrandsAgents support defining tools as async functions? If not, I would like to request support for async tool definitions to better integrate with asynchronous workflows.
Proposed Solution
No response
Use Case
Many modern applications use asynchronous operations for network-bound tasks like API calls or database queries. For example: • Calling an external LLM API such as OpenAI or Claude, which requires awaiting the HTTP response • Performing a vector search against an async-compatible database or search engine (e.g., Redis, Qdrant, or OpenSearch via async clients) • Fetching contextual information from other services in parallel
Allowing tools to be defined as async def would enable developers to: • Seamlessly integrate StrandsAgents into async applications without blocking the event loop • Compose multiple async tools in parallel using asyncio.gather • Improve responsiveness and scalability in production environments
Alternatives Solutions
No response
Additional Context
No response
+1
Thank you for opening this feature request @tan-z-tan.
This is something that the team is actively thinking about. https://github.com/strands-agents/sdk-python/issues/83 is a little related as well so tagging that here.
+1
Notes on Async support for Tools in Strands
A brief write up on the unsystematic hacking/patching to support async support for tool invocation in the Strands Agents Python SDK. It covers the incremental patches applied, the challenges encountered, the final outcome. Also, outlined is a comparison against the goals in EPIC #129 (“Implement native async iterator support”).
My original motiviation was to support async and parallel invocation of agents for processing RSS feeds. I had created a RSS Feed Parser using the @tool decorator. Without paying attention, I had started building the RSS Feed tool (using the @tool decorator) as an async script, and it failed. That piqued my curiousity to see if I can patch or hack to make it work. I had done something similar but much smaller hack when I had found that the A2A Client Python code was not using true async libraries, so that A2A client would block the event loop.
Hacking | Patching Journey & Key Milestones
Each of the steps in this journey happened incrementally. That is, the change was not systematic or done holistically. Changes were made one step or file at a time as bugs appeared during execution with generous help from Q!
decorator.py
- Modified
src/strands/tools/decorator.pyso that: - If a user function is an
async def, the generated wrapper is alsoasync defandawaits the underlying coroutine.- Detect using
inspect.iscoroutinefunction(f)on the user’s function. - Emit an
async def wrapperwhen the underlying function is asynchronous, invokingawait f(...). - Leave sync functions untouched, so existing tools continue to work. (however the sync/normal version of the script does not work. I still have to debug and figure out why it fails.)
- Detect using
event_loop.py
- Converted core routines in
src/strands/event_loop/event_loop.pytoasync def:event_loop_cycle_handle_tool_executionrecurse_event_loop
- Replace all recursive “return
foo(...)” calls withreturn await foo(...). - Wrap the tool handler via a small async helper so that it can
awaitdecorator-generated coroutines.
executor.py
- Transformed
run_toolsinsrc/strands/tools/executor.pyinto anasync deffunction. - Within both sequential and (when enabled) parallel paths, inspect each handler return for coroutines and
awaitthem. - Sequential path loops over
tool_uses,await handler(tool_use). - Parallel path is commented out so that the processing flow goes through the sequential logic. Before commenting out the
asyncexecution would fail. I decided not to patch or debug the parallel handler and make itasync. No debug attempted (or an attempt to understand) on why control was going to parallel executor.
agent.py
- In
src/strands/agent/agent.py, both the public__call__and internal_run_loop/_execute_event_loop_cyclemethods becameasync def.
streaming.py
- No changes. Only used for instrumenting with logs to understand flow and debug. Bugs were manifesting as if Bedrock Streaming was returning results with no content (tied to the parallel path execution.)
- Testing
- Instrumented debug prints around streaming and tool-use phases to confirm that all coroutines were awaited and that messages contained the expected content.
- Verified end-to-end with
newrss.py, confirming that anasync defRSS-parsing tool executed without errors and produced the correct output. - The sync/normal version of the script still fails, so if tools created with decorator do not have
async&await- it fails.
Comparison to EPIC #129
| EPIC Item | Status | Notes |
|---|---|---|
| 1. Async iterator event loop | ◐ | The loop is now async/await, but still recursive rather than an async for interface. |
| 2. Async tool invocation | ✅ | @tool supports async def, and executor awaits handler coroutines. |
| 3. Async model streams | ⬜ | Providers remain synchronous generators; full async streams remain future work. |
| 4. Cancellation tokens | ⬜ | No change. |
| 5. Async MCP servers | ⬜ | No change. |
| 6. Async‐aware AgentResult | ◐ | AgentResult can be awaited in async contexts; streaming helpers could be improved. |
| 7. Bidirectional streaming | ⬜ | |
| 8. Sync wrappers for backward compatibility | ◐ | Attempted to keep backward compatibility preserved via sync‐wrapper or optional helper functions. However normal synchronous calls not working in testing. |
| 9. Contextvar propagation in tests | ⬜ | No formal testing, no updates to testing. |
Async tools are now supported and documented at https://strandsagents.com/latest/documentation/docs/user-guide/concepts/tools/python-tools/#async-invocation
Nice! @zastrowm would this enable to stream sub-agent's events for agents-as-tools pattern?
I found that only supervisor's tokens are surfaced when calling agent.stream_async()
Nice! @zastrowm would this enable to stream sub-agent's events for agents-as-tools pattern?
Not yet. The refactor to enable async tools provided the ability to enable tool result streaming but we haven't yet implemented it - We think tool result streaming will be the first implementation of #242.
CC @pgrayy