Skip to main content

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)
  • The Droid CLI on your PATH (droid)
  • A local repository to chat with

Installation

After the browser login, droid exec works from your app without needing API keys in code.

Try the Official Example

Open http://localhost:4000 - you’ll see a chat window overlaid on the repo’s README.

2. Why Use Droid Exec?

Building AI features that understand codebases requires orchestrating multiple operations: searching files, reading code, analyzing structure, and synthesizing answers. Without droid 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: spawn droid 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 json for final output only
-m (model): Choose your AI model
  • glm-4.7 - Fast, cheap (default)
  • gpt-5-codex - Most powerful for complex code
  • claude-sonnet-4-5-20250929 - Best balance of speed and capability
-r (reasoning): Control thinking depth
  • off - No reasoning, fastest
  • low - Light reasoning (default)
  • medium|high - Deeper analysis, slower
No --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:
  1. Searched the codebase with ripgrep for “MCP”, “charge”, “payment”
  2. Read relevant files (billing config, pricing logic, env vars)
  3. Analyzed the code structure to understand the charging flow
  4. Synthesized a complete answer with file locations, variable names, and implementation details
All streamed in real-time through SSE - no manual orchestration needed.

Project Structure (from the Example)

Configuration Options

The example supports environment variables:

Best Practices

Do:
  • Use read-only mode (no --auto flag) 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
⚠️ Avoid:
  • Using --auto medium/high in 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:
Databases:
Websites:

Change Models on the Fly

Add Tool Call Visibility

The example’s stream.ts parses debug events. You can surface them in the UI:
This creates a transparent, trust-building experience where users see exactly what the agent is doing.

Additional Resources

Official Example: Documentation: Community:

Next Steps

  1. Clone the example: git clone https://github.com/Factory-AI/examples.git
  2. Run it locally: cd examples/droid-chat && bun dev
  3. Explore the code in src/server/chat.ts to see how SSE streaming works
  4. Customize src/server/prompt.ts to change the agent’s behavior
  5. Swap ./repos/ content to chat with your own repositories
The example is intentionally minimal (~500 lines total) so you can understand it fully and adapt it to your needs.