What this doc covers
- How to build a “chat with repo” feature using Factory’s Droid Exec in headless mode
- Setting up streaming responses with Server-Sent Events for real-time agent feedback
- Understanding the actual implementation from Factory’s official example
For a complete reference on Droid Exec capabilities, see the Droid Exec Overview
1. Pre-requisites and Installation
Requirements
- Bun (the example uses Bun, but Node.js works too)
- Factory CLI installed (
droidon your PATH) - A local repository to chat with
Installation
droid exec works from your app without needing API keys in code.
Try the Official Example
2. Why Use Droid Exec?
Building AI features that understand codebases requires orchestrating multiple operations: searching files, reading code, analyzing structure, and synthesizing answers. Withoutdroid exec, a question like “How do we charge for MCP servers?” would require dozens of API calls and custom logic to search, read, and understand the relevant code.
droid exec is Factory’s headless CLI mode that handles this autonomously in a single command. It searches codebases, reads files, reasons about code structure, and returns structured JSON output—with built-in safety controls and configurable autonomy levels. Perfect for building chat interfaces, CI/CD automation, or any application that needs codebase intelligence.
3. How It Works: The Core Pattern
The Factory example uses a simple pattern: spawndroid exec with --output-format debug and stream the results via Server-Sent Events (SSE).
Running Droid Exec
Key Flags Explained
--output-format debug: Streams structured events as the agent works
- Each tool call (file read, search, etc.) emits an event
- Lets you show real-time progress to users
- Alternative:
--output-format jsonfor final output only
-m (model): Choose your AI model
glm-4.6- Fast, cheap (default)gpt-5-codex- Most powerful for complex codeclaude-sonnet-4-5- Best balance of speed and capability
-r (reasoning): Control thinking depth
off- No reasoning, fastestlow- Light reasoning (default)medium|high- Deeper analysis, slower
--auto flag?: Defaults to read-only (safest)
- Can’t modify files, only read/search/analyze
- Perfect for chat applications
See CLI Reference for all flag explanations
4. Building the Chat Feature: Streaming with SSE
The Factory example streams agent activity in real-time using Server-Sent Events. This gives users immediate feedback as the agent searches, reads files, and thinks.Server: Streaming SSE Endpoint
Event Types You’ll Receive
When--output-format debug is used, droid emits events like:
Client: React Hook for SSE
Real-World Example: The Video
In the demo video, the user asked: “Can you search for how we charge for MCP servers?” Behind the scenes,droid exec automatically:
- Searched the codebase with ripgrep for “MCP”, “charge”, “payment”
- Read relevant files (billing config, pricing logic, env vars)
- Analyzed the code structure to understand the charging flow
- Synthesized a complete answer with file locations, variable names, and implementation details
Project Structure (from the Example)
Configuration Options
The example supports environment variables:Best Practices
✅ Do:- Use read-only mode (no
--autoflag) for user-facing features - Validate user input before passing to
droid exec - Set timeouts (example uses 240 seconds)
- Parse SSE events incrementally for responsive UI
- Strip local file paths from debug output before sending to client
- Using
--auto medium/highin production without sandboxing - Passing unsanitized user input directly to the CLI
- Blocking the main thread while waiting for results
5. Customization & Extensions
Swap the Data Source
The example ships with a local repo, but you can easily adapt it: PDFs & Documents:Change Models on the Fly
Add Tool Call Visibility
The example’sstream.ts parses debug events. You can surface them in the UI:
Additional Resources
Official Example:- GitHub: droid-chat example - Full working code
- Droid Exec Overview - Complete CLI reference
- Autonomy Levels Guide - Understanding
--autoflags - CI/CD Cookbook - Production patterns
- Model Configuration - Available models and settings
- Factory Discord - Get help from the team
- GitHub Discussions - Share your builds
Next Steps
- Clone the example:
git clone https://github.com/Factory-AI/examples.git - Run it locally:
cd examples/droid-chat && bun dev - Explore the code in
src/server/chat.tsto see how SSE streaming works - Customize
src/server/prompt.tsto change the agent’s behavior - Swap
./repos/content to chat with your own repositories