A Research Agent powered by Resonate and OpenAI, running on Cloudflare Workers. The Research Agent is a distributed, recursive agent that breaks a research topic into subtopics, researches each subtopic recursively, and synthesizes the results.
This example demonstrates how complex, distributed agentic applications can be implemented with simple code in Resonate's Distributed Async Await: The research agent is a recursive generator function that breaks down topics into subtopics and invokes itself for each subtopic:
function* research(ctx, topic, depth) {
const messages = [
{ role: "system", content: "Break topics into subtopics..." },
{ role: "user", content: `Research ${topic}` }
];
while (true) {
// Ask the LLM about the topic
const response = yield* ctx.run(prompt, messages, ...);
messages.push(response);
// If LLM wants to research subtopics...
if (response.tool_calls) {
const handles = [];
// Spawn parallel research for each subtopic
for (const tool_call of response.tool_calls) {
const subtopic = ...;
const handle = yield* ctx.beginRpc(research, subtopic, depth - 1);
handles.push([tool_call, handle]);
}
// Wait for all subtopic results
for (const [tool_call, handle] of handles) {
const result = yield* handle;
messages.push({ role: "tool", ..., content: result });
}
} else {
// LLM provided final summary
return response.content;
}
}
}The following video visualizes how this recursive pattern creates a dynamic call graph, spawning parallel research branches that fan out as topics are decomposed, then fan back in as results are synthesized:
call-graph.mov
Key concepts:
- Concurrent Execution: Multiple subtopics are researched concurrently via
ctx.beginRpc - Coordination: Handles are collected first, then awaited together (fork/join, fan-out/fan-in)
- Depth control: Recursion stops when
depthreaches 0
You can run the Deep Research Agent locally on your machine with Cloudflare's Wrangler or you can deploy the agent to Cloudflare Platform.
Install the Resonate Server & CLI with Homebrew or download the latest release from Github.
brew install resonatehq/tap/resonate
To run this project you also need an OpenAI API Key and export the key as an environment variable
export OPENAI_API_KEY="sk-..."
Start the Resonate Server. By default, the Resonate Server will listen at http://localhost:8001.
resonate dev
Clone the repository
git clone https://github.com/resonatehq-examples/example-openai-deep-research-agent-cloudflare-ts
cd example-openai-deep-research-agent-cloudflare-ts
Install dependencies
npm install
Start the Cloudflare Function. By default, the Cloudflare Function will listen at http://localhost:8080.
npm run dev
Start a research task
resonate invoke <promise-id> --func research --arg <topic> --arg <depth> --target <function-url>
Example
resonate invoke research.1 --func research --arg "What are distributed systems" --arg 1 --target http://localhost:8787
Use the resonate tree command to visualize the research execution.
resonate tree research.1
This section guides you through deploying the Deep Research Agent to Cloudflare Platform using Cloud Run for the Resonate server and Cloud Functions for the research function.
Install the Resonate CLI with Homebrew or download the latest release from Github.
brew install resonatehq/tap/resonate
To run this project you need an OpenAI API Key.
Ensure you have a Cloudflare Platform account.
Warning
Cloudflare Platform offers extensive configuration options. The instructions in this guide provide a baseline setup that you will need to adapt for your specific requirements, organizational policies, or security constraints.
Expose the Resonate server running locally to the cloud
Step 1: cloudflare tunnel url (you can use ngrok or similar)
cloudflared tunnel --url http://localhost:8001
Step 2: Run the server locally
Configure the Resonate Server with its URL.
resonate dev --system-url <tunnel-url>
Example
resonate dev --system-url https://purple-lion-2q3j4j.trycloudflare.com
Paste the wrangler.toml at the root of this directory (replace the name with the name of your newly created worker)
name = "XXXXX-XXXXX-XXXX"
main = "src/index.ts"
compatibility_date = "2024-01-01"Deploy it
npm run deploy
Start a research task
resonate invoke <promise-id> --func research --arg <topic> --arg <depth> --target <function-url>
Example
resonate invoke research.1 --func research --arg "What are distributed systems" --arg 1 --target https://xxxxx-xxxxx-xxxx.abc.workers.dev
Use the resonate tree command to visualize the countdown execution.
resonate tree research.1
research.1
βββ research.1.0 π’ (run)
βββ research.1.1 π‘ (rpc research)
β βββ research.1.1.0 π‘ (run)
βββ research.1.2 π‘ (rpc research)
β βββ research.1.2.0 π‘ (run)
βββ research.1.3 π‘ (rpc research)
βββ research.1.3.0 π‘ (run)
If you are still having trouble please open an issue.

