A Research Agent powered by Resonate and OpenAI, running on Supabase Edge Functions. 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 Supabase CLI or you can deploy the agent to Supabase Platform.
Install the Resonate Server & CLI with Homebrew or download the latest release from Github.
brew install resonatehq/tap/resonate
Install the Supabase CLI
To run this project you will also need to add an OpenAI API Key to the supabase/functions/.env file. See the supabase docs for more information.
supabase start
Clone the repository
git clone https://github.com/resonatehq-examples/example-openai-deep-research-agent-supabase-ts
cd example-openai-deep-research-agent-supabase-ts
Install dependencies
npm install
supabase functions serve deep-research-agent
resonate dev --system-url http://host.docker.internal:8001
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://127.0.0.1:54321/functions/v1/deep-research-agent
Use the resonate tree command to visualize the research 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)
You can also see the result with resonate promises get:
resonate promises get research.1
Install the Supabase CLI
supabase secrets set --env-file supabase/functions/.env
supabase functions deploy deep-research-agent --project-ref <PROJECT_ID>
ngrok http 8001
resonate dev --system-url <ngrok-url>
Example
resonate dev --system-url https://583ef7749990.ngrok-free.app
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://<orgId>.supabase.co/functions/v1/deep-research-agent
Use the resonate tree command to visualize the countdown execution.
resonate tree research.1
Example output (while waiting on the second sleep):
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.4 🟢 (rpc research)
│ └── research.1.4.0 🟢 (run)
└── research.1.5 🟢 (rpc research)
└── research.1.5.0 🟢 (run)
You can also see the result with resonate promises get:
resonate promises get research.1
If you are still having trouble please open an issue.
