展示HN:持续Claude – 循环运行Claude代码
Show HN: Continuous Claude – run Claude Code in a loop

原始链接: https://github.com/AnandChowdhary/continuous-claude

## 持续Claude:自动化AI驱动的开发 持续Claude是一个CLI工具,旨在通过循环运行Claude代码来自动化复杂的编码项目。它解决了单次AI编码的局限性,通过实现迭代改进和持久化上下文来解决问题——这对于处理大型任务(如添加广泛的单元测试或迁移代码库)至关重要。 该工具模拟CI/CD实践,自动创建PR,等待检查和批准,并合并代码。一个关键特性是持久化上下文机制,利用共享的markdown文件(`SHARED_TASK_NOTES.md`)来存储迭代之间的进度和见解,防止知识丢失。 用户提供提示,设置迭代限制或成本预算,并指定GitHub仓库。然后,脚本管理整个PR生命周期,Claude专注于每个步骤的增量进展。它适用于依赖项更新、重构和解决技术债务等任务,并通过PR审查提供人工参与的方案。 持续Claude可在[GitHub](https://github.com/AnandChowdhary/continuous-claude)上获取,并可以通过一条命令安装。

## 持续Claude:迭代编码代理 Anandchowdhary 创建了“持续Claude”,一个CLI工具,旨在以连续、迭代的方式运行Claude代码,用于软件开发任务。与典型的单次编码代理不同,该工具在整个项目过程中保持持久的上下文。 它自动化了基于PR的工作流程:创建分支、应用代码更改、提交、打开拉取请求、等待审查和检查,以及合并成功的更改——所有这些都由Claude驱动。进度和推理记录在共享笔记中,防止迭代之间的数据丢失。 这种方法特别适用于复杂的、多步骤的任务,例如重构、依赖升级、增加测试覆盖率或框架迁移,在这些任务中保留中间步骤至关重要。一篇详细的博客文章进一步解释了该工具的功能。
相关文章

原文
Automated workflow that orchestrates Claude Code in a continuous loop, autonomously creating PRs, waiting for checks, and merging - so multi-step projects complete while you sleep.

This all started because I was contractually obligated to write unit tests for a codebase with hundreds of thousands of lines of code and go from 0% to 80%+ coverage in the next few weeks - seems like something Claude should do. So I built Continuous Claude, a CLI tool to run Claude Code in a loop that maintains a persistent context across multiple iterations.

Current AI coding tools tend to halt after completing a task once they think the job is done and they don't really have an opportunity for self-criticism or further improvement. And this one-shot pattern then makes it difficult to tackle larger projects. So in contrast to running Claude Code "as is" (which provides help in isolated bursts), what you want is to run Claude code for a long period of time without exhausting the context window.

Turns out, it's as simple as just running Claude Code in a continuous loop - but drawing inspiration from CI/CD practices and persistent agents - you can take it a step further by running it on a schedule or through triggers and connecting it to your GitHub pull requests workflow. And by persisting relevant context and results from one iteration to the next, this process ensures that knowledge gained in earlier steps is not lost, which is currently not possible in stateless AI queries and something you have to slap on top by setting up markdown files to store progress and context engineer accordingly.

While + git + persistence

When you have a task that is too complex to complete in a single step, for example migrating from Next.js to TanStack Start or adding documentation for a previously undocumented codebase, you need to break down the task into many small tasks (e.g., only document this one file first, then the rest of the folder, and so on). So all you need is a while loop that stops eventually, a version control and PR reviews integrations, and a persistent context mechanism.

I wrote a Bash script that acts as the conductor, repeatedly invoking Claude Code with the appropriate prompts and handling the surrounding tooling. The script accepts the prompt (task description), the maximum number of iterations to run, and repository information (owner and repo name for the GitHub integration). When started, it goes into a loop (potentially infinite) where each loop iteration corresponds to Claude attempting a unit of work towards the overall goal.

This loop runs Claude Code with the prompt that the user supplies but also explicitly tells the model: "This is part of a continuous development loop... you don't need to complete the entire goal in one iteration, just make meaningful progress on one thing, then leave clear notes for the next iteration (human or AI)... think of it as a relay race where you're passing the baton." This sets the right expectations for the model that it should aim for incremental progress, not rush to finish everything at once.

The script handles also the git operations around the code changes. For each loop, it creates a new branch, generates the commit, pushes it, and creates a pull request using GitHub's CLI. Next step is monitoring and control: it enters a time-interval loop where it periodically checks the status of CI checks and reviews on that PR using gh pr checks and waits for the PR to get all green statuses and required approvals. If everything looks good (tests passed, etc.), it merges the PR, then pulls the updated main branch and cleans up the local feature branch, so essentially implementing a merge queue.

Should an iteration fail, it can just close the PR, delete the branch, and disregard the work. This is indeed wasteful, but now with knowledge of the test failures, the next attempt can choose to try something different. And because it piggybacks on top of GitHub, you can combine things like code review and preview environments without any additional work, for example if the repo requires at least one code owner review or has specific CI checks that must pass, it will simply be constrained by those and will not merge until they are satisfied.

It's a common practice to use a shared persistent markdown file like TASKS.md to maintain context continuity across different agents working on the same task. The MVP of this is just a single file that serves as an external memory where Claude can record what it has done and what should be done next, any new insights or mistakes, and so on.

The default prompt includes guidelines about what these notes should contain: they should be concise and action-oriented, focusing on context that will help the next step, and should not become a verbose log or include information that can be deduced from the code or test results GitHub. Without these specific prompting instructions, it would go rogue and create a really long file that harms more than helps, when the intent is to keep the notes as a clean handoff package between runs.

An actual production example I saw was that the previous iteration ended with "Note: I tried adding tests to X but they paid because of an edge case, need to handle null input in function Y" and the very next Claude invocation saw that and prioritized addressing it. I think you can definitely have a much better mechanism but I found that a single tiny file already reduces the chances of context drift, where it might forget earlier reasoning or work and go in circles.

I am not yet sure whether I want this file to be checked into the git commit history. This file serves as an external scratchpad for Claude, giving it a long-term memory that survives beyond the context window of any single invocation, so one can argue that it makes sense to have a more sophisticated system that can manage multiple tasks while tracking the changes to the living document, but I'm not sure yet. Either way, the trade-off is that the notes rely on Claude to faithfully maintain them (which I observed it generally will because of the prompting).

I was thinking about what could be some other use cases for this. Tools like Renovate and Dependabot were the first to come to mind, while Continuous Code is not limited to only dependencies and also capable of fixing post-update issues. So you could run a GitHub Actions workflow that runs every morning, checks for dependency updates, and then continuously makes updates to the codebase based on the release notes of the dependency until you're all green again.

Also refactoring tasks like breaking a monolith into modules, modernizing callbacks to async/await, or updating code to new style guidelines can be done stepwise. For example, it could over a weekend perform a series of 20 pull requests, each doing part of the refactor, and each PR will have to pass all tests/CI. There's a whole class of tasks that are too mundane for us but still require attention to avoid breaking the build - can this be how we address technical debt?

In general, I think that the model of running the coding agent in a loop better mirrors human development practices and avoids the common pitfall it trying to solve everything in one go; even with plan mode, I think iterative refinement is the way to go for larger tasks. Also depending on your appetite, the system can be as human-in-the-loop as you like. Claude Code handles the grunt work of coding and iteration, but humans remain in the loop through familiar mechanisms like PR reviews and reading the markdown notes.

Continuous Claude emphasizes small, incremental changes rather than giant leaps. Download the CLI from GitHub to get started!

Using Claude Code to drive iterative development, this script fully automates the PR lifecycle from code changes through to merged commits:

  • Claude Code runs in a loop based on your prompt
  • All changes are committed to a new branch
  • A new pull request is created
  • It waits for all required PR checks and code reviews to complete
  • Once checks pass and reviews are approved, the PR is merged
  • This process repeats until your task is complete
  • A SHARED_TASK_NOTES.md file maintains continuity by passing context between iterations, enabling seamless handoffs across AI and human developers

Install with a single command:

curl -fsSL https://raw.githubusercontent.com/AnandChowdhary/continuous-claude/main/install.sh | bash

This will:

  • Install continuous-claude to ~/.local/bin
  • Check for required dependencies
  • Guide you through adding it to your PATH if needed

If you prefer to install manually:

# Download the script
curl -fsSL https://raw.githubusercontent.com/AnandChowdhary/continuous-claude/main/continuous_claude.sh -o continuous-claude

# Make it executable
chmod +x continuous-claude

# Move to a directory in your PATH
sudo mv continuous-claude /usr/local/bin/

To uninstall continuous-claude:

rm ~/.local/bin/continuous-claude
# or if you installed to /usr/local/bin:
sudo rm /usr/local/bin/continuous-claude

Before using continuous-claude, you need:

  1. Claude Code CLI - Authenticate with claude auth
  2. GitHub CLI - Authenticate with gh auth login
  3. jq - Install with brew install jq (macOS) or apt-get install jq (Linux)
# Run with your prompt, max runs, and GitHub repo
continuous-claude --prompt "add unit tests until all code is covered" --max-runs 5 --owner AnandChowdhary --repo continuous-claude

# Or run with a cost budget instead
continuous-claude --prompt "add unit tests until all code is covered" --max-cost 10.00 --owner AnandChowdhary --repo continuous-claude
  • -p, --prompt: Task prompt for Claude Code (required)
  • -m, --max-runs: Maximum number of iterations, use 0 for infinite (required unless --max-cost is provided)
  • --max-cost: Maximum USD to spend (required unless --max-runs is provided)
  • --owner: GitHub repository owner (required)
  • --repo: GitHub repository name (required)
  • --merge-strategy: Merge strategy: squash, merge, or rebase (default: squash)
  • --git-branch-prefix: Prefix for git branch names (default: continuous-claude/)
  • --notes-file: Path to shared task notes file (default: SHARED_TASK_NOTES.md)
  • --disable-commits: Disable automatic git commits, PR creation, and merging (useful for testing)
  • --worktree <name>: Run in a git worktree for parallel execution (creates if needed)
  • --worktree-base-dir <path>: Base directory for worktrees (default: ../continuous-claude-worktrees)
  • --cleanup-worktree: Remove worktree after completion
  • --list-worktrees: List all active git worktrees and exit

Any additional flags you provide that are not recognized by continuous-claude will be automatically forwarded to the underlying claude command. For example, you can pass --allowedTools, --model, or any other Claude Code CLI flags.

# Run 5 iterations
continuous-claude -p "improve code quality" -m 5 --owner AnandChowdhary --repo continuous-claude

# Run infinitely until stopped
continuous-claude -p "add unit tests until all code is covered" -m 0 --owner AnandChowdhary --repo continuous-claude

# Run until $10 budget exhausted
continuous-claude -p "add documentation" --max-cost 10.00 --owner AnandChowdhary --repo continuous-claude

# Run max 10 iterations or $5, whichever comes first
continuous-claude -p "refactor code" -m 10 --max-cost 5.00 --owner AnandChowdhary --repo continuous-claude

# Use merge commits instead of squash
continuous-claude -p "add features" -m 5 --owner AnandChowdhary --repo continuous-claude --merge-strategy merge

# Use rebase strategy
continuous-claude -p "update dependencies" -m 3 --owner AnandChowdhary --repo continuous-claude --merge-strategy rebase

# Use custom branch prefix
continuous-claude -p "refactor code" -m 3 --owner AnandChowdhary --repo continuous-claude --git-branch-prefix "feature/"

# Use custom notes file
continuous-claude -p "add features" -m 5 --owner AnandChowdhary --repo continuous-claude --notes-file "PROJECT_CONTEXT.md"

# Test without creating commits or PRs
continuous-claude -p "test changes" -m 2 --owner AnandChowdhary --repo continuous-claude --disable-commits

# Pass additional Claude Code CLI flags (e.g., restrict tools)
continuous-claude -p "add features" -m 3 --owner AnandChowdhary --repo continuous-claude --allowedTools "Write,Read"

# Use a different model
continuous-claude -p "refactor code" -m 5 --owner AnandChowdhary --repo continuous-claude --model claude-haiku-4-5

Use git worktrees to run multiple instances simultaneously without conflicts:

# Terminal 1
continuous-claude -p "Add unit tests" -m 5 --owner myuser --repo myproject --worktree tests

# Terminal 2 (simultaneously)
continuous-claude -p "Add docs" -m 5 --owner myuser --repo myproject --worktree docs

Each instance creates its own worktree at ../continuous-claude-worktrees/<name>/, pulls the latest changes, and runs independently. Worktrees persist for reuse.

# List worktrees
continuous-claude --list-worktrees

# Clean up after completion
continuous-claude -p "task" -m 1 --owner user --repo project --worktree temp --cleanup-worktree

Here's what a successful run looks like:

🔄 (1/1) Starting iteration...
🌿 (1/1) Creating branch: continuous-claude/iteration-1/2025-11-15-be939873
🤖 (1/1) Running Claude Code...
📝 (1/1) Output: Perfect! I've successfully completed this iteration of the testing project. Here's what I accomplished: [...]
💰 (1/1) Cost: $0.042
✅ (1/1) Work completed
🌿 (1/1) Creating branch: continuous-claude/iteration-1/2025-11-15-be939873
💬 (1/1) Committing changes...
📦 (1/1) Changes committed on branch: continuous-claude/iteration-1/2025-11-15-be939873
📤 (1/1) Pushing branch...
🔨 (1/1) Creating pull request...
🔍 (1/1) PR #893 created, waiting 5 seconds for GitHub to set up...
🔍 (1/1) Checking PR status (iteration 1/180)...
   📊 Found 6 check(s)
   🟢 2    🟡 4    🔴 0
   👁️  Review status: None
⏳ Waiting for: checks to complete
✅ (1/1) All PR checks and reviews passed
🔀 (1/1) Merging PR #893...
📥 (1/1) Pulling latest from main...
🗑️ (1/1) Deleting local branch: continuous-claude/iteration-1/2025-11-15-be939873
✅ (1/1) PR merged and local branch cleaned up
🎉 Done with total cost: $0.042

MIT ©️ Anand Chowdhary

联系我们 contact @ memedata.com