# What Is Delegation Mode in OpenCodeReview? How It Works With AI Agents

> Discover delegation mode in OpenCodeReview. Learn how OCR selects files and creates review specs without LLMs, offloading AI analysis to host agents like Claude Code.

- Repository: [Alibaba/open-code-review](https://github.com/alibaba/open-code-review)
- Tags: deep-dive
- Published: 2026-08-06

---

**Delegation mode is a specialized operating mode where OpenCodeReview (OCR) selects files and generates deterministic review specifications without ever calling a Large Language Model, shifting the AI analysis to a host agent such as Claude Code.**

Delegation mode in the `alibaba/open-code-review` repository redefines automated code review by separating file selection and rule resolution from AI execution. Instead of configuring API keys for LLM providers, OCR prepares a structured review specification that external AI agents consume and process. This architecture enables seamless integration with trusted AI environments while eliminating credential management overhead on the OCR side.

## How Delegation Mode Works in OpenCodeReview

When running in delegation mode, OCR acts as a deterministic preprocessor rather than an AI client. The workflow follows five distinct stages:

1. **Local Diff Computation**: OCR analyzes the repository to identify changed files and compute the diff.
2. **Rule Resolution**: The rule resolver evaluates hierarchical configuration (project-level, global, system) to determine applicable review rules for each file.
3. **Deterministic Grouping**: Files sharing identical rule content are grouped together using the `GroupRules` function in [`internal/delegate/rulegroup.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/rulegroup.go).
4. **Specification Generation**: The tool renders a Markdown review specification via `RuleGroupsMarkdown` in [`internal/delegate/format.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/format.go), enumerating files and their associated rules.
5. **Host Agent Execution**: An external AI agent receives the specification, invokes its configured LLM, and posts the resulting comments to the code host.

Because OCR never initiates LLM calls, **no API keys or endpoint configurations** are required for the OpenCodeReview installation.

## Key Architectural Components of Delegation Mode

Understanding the delegation architecture requires examining four critical components that handle rule processing and specification delivery.

### The Delegate Package

The `internal/delegate` package contains the core logic for preparing review specifications. The `GroupRules` function accepts a rule resolver and file paths, returning a slice of `RuleGroup` structs that cluster files by identical resolved rule text. This deterministic grouping ensures that the host agent processes semantically similar files together.

### Rule Resolution Engine

The rule resolver implements the `rules.Resolver` interface to fetch rule text from hierarchical configuration sources. When `delegate.GroupRules` executes, it leverages this resolver to normalize rule content across different configuration layers, ensuring consistent evaluation regardless of where rules are defined.

### CLI Command Interface

The [`cmd/opencodereview/delegate_cmd.go`](https://github.com/alibaba/open-code-review/blob/main/cmd/opencodereview/delegate_cmd.go) file implements the `ocr delegate` command with two primary sub-commands:

- **`preview`**: Lists files scheduled for review along with mode metadata and references
- **`rule`**: Outputs the grouped rule specifications as formatted Markdown

### Host Agent Integration

The host agent—such as Claude Code or custom AI environments—consumes the Markdown specification produced by OCR. This agent holds the LLM credentials and handles the actual code analysis, maintaining a clear separation between OpenCodeReview's deterministic preprocessing and AI-powered evaluation.

## Using Delegation Mode With AI Agents

Delegation mode integrates specifically with agentic AI environments that can consume structured specifications. Claude Code serves as the reference implementation for this workflow.

First, install the OCR delegate skill in your Claude Code environment:

```bash
npx skills add alibaba/open-code-review --skill open-code-review-delegate

```

Generate a preview of files to be reviewed between two branches:

```bash
ocr delegate preview --from main --to feature

```

Produce the deterministic review specification for specific files:

```bash
ocr delegate rule src/main.go src/utils/helpers.go

```

When executed within Claude Code, the agent automatically receives the specification, invokes its configured LLM to analyze the code against the provided rules, and posts review comments directly to your code host.

## Programmatic Implementation in Go

Developers can leverage the delegation API directly within Go applications by importing the `internal/delegate` package. This approach enables custom workflows that generate review specifications programmatically.

```go
package main

import (
    "fmt"
    "github.com/alibaba/open-code-review/internal/delegate"
    "github.com/alibaba/open-code-review/internal/config/rules"
)

func main() {
    // Initialize your rule resolver implementation
    var resolver rules.Resolver // e.g., config.NewResolver()
    
    // Define paths to review
    paths := []string{"src/main.go", "src/utils/helpers.go"}
    
    // Group files by identical rule content
    groups := delegate.GroupRules(resolver, paths)
    
    // Render Markdown specification for the host agent
    spec := delegate.RuleGroupsMarkdown(groups)
    fmt.Println(spec)
}

```

The `delegate.GroupRules` function performs the heavy lifting of clustering files based on resolved rule metadata, while `delegate.RuleGroupsMarkdown` handles the final formatting output specified in [`internal/delegate/format.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/format.go).

## Summary

Delegation mode transforms OpenCodeReview from an all-in-one AI tool into a specialized preprocessor for agentic workflows:

- **Zero LLM configuration** required on the OCR side, eliminating API key management
- **Deterministic rule grouping** via [`internal/delegate/rulegroup.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/rulegroup.go) ensures consistent file clustering
- **Markdown specification generation** through [`internal/delegate/format.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/format.go) provides structured input for AI agents
- **Seamless Claude Code integration** via the `ocr delegate` command set enables external AI processing
- **Clean architectural separation** between file selection (OCR) and intelligent analysis (host agent)

## Frequently Asked Questions

### What is the primary advantage of using delegation mode?

Delegation mode eliminates the need to configure LLM API keys within OpenCodeReview by outsourcing AI execution to a host agent. This architecture is ideal for organizations that already maintain trusted AI environments like Claude Code and want to avoid duplicating credential management across multiple tools.

### Which files handle the rule grouping logic in delegation mode?

The deterministic grouping of files by rule content is implemented in [`internal/delegate/rulegroup.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/rulegroup.go), specifically within the `GroupRules` function. The subsequent Markdown rendering of these groups occurs in [`internal/delegate/format.go`](https://github.com/alibaba/open-code-review/blob/main/internal/delegate/format.go) via the `RuleGroupsMarkdown` function.

### Can I use delegation mode with AI agents other than Claude Code?

Yes. Delegation mode produces a standard Markdown specification that any AI agent capable of consuming structured documents can process. While Claude Code provides a native integration via the `open-code-review-delegate` skill, custom agents can parse the output of `ocr delegate rule` and invoke their own LLM backends.

### How do I switch between standard mode and delegation mode?

Standard OCR commands invoke LLMs directly using configured credentials. To activate delegation mode, use the `ocr delegate` sub-commands (`preview` or `rule`) implemented in [`cmd/opencodereview/delegate_cmd.go`](https://github.com/alibaba/open-code-review/blob/main/cmd/opencodereview/delegate_cmd.go). These commands bypass LLM calls entirely and output the review specification for external consumption.