Reliable fallback technique for writing large file contents when write_file and shell_agent both fail with 'unknown error' due to payload size limits — uses run
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-large-file-write-heredoc-49eaa56d5efb ,按照其中的说明把「large-file-write-heredoc」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
When writing large files (typically several KB or more), two common tools may
fail with unknown error due to internal payload size limits:
write_file — has a maximum content size it can handle in a single call.shell_agent — may also hit payload limits when the task description
includes large inline content.Use run_shell with a Python heredoc pattern. This streams the file
content through stdin directly into Python's open(), bypassing the payload
constraints of the other tools.
python3 - << 'EOF'
content = """<FILE CONTENTS HERE>"""
with open("<TARGET PATH>", "w") as f:
f.write(content)
EOF
Pass this as the command parameter to run_shell.
Attempt the normal write using write_file first. If it succeeds, you
are done.
If write_file fails (especially with unknown error or a timeout on
large content), do NOT retry with shell_agent using inline content —
it will likely fail for the same reason.
Use the run_shell heredoc fallback:
open() call.command to run_shell.Escape carefully inside the heredoc:
\\).\"\"\").EOF must not appear on a line by itself inside
the content (rename it to PYEOF or FILEEOF if needed).Verify the write by following up with a run_shell call such as:
wc -l <TARGET PATH> && head -5 <TARGET PATH>
Suppose you need to write a large TypeScript file to
src/components/Dashboard.ts:
python3 - << 'PYEOF'
content = """import { foo } from './foo';
export interface DashboardData {
title: string;
items: string[];
}
export function createDashboard(data: DashboardData): string {
return `<div>${data.title}</div>`;
}
"""
with open("src/components/Dashboard.ts", "w") as f:
f.write(content)
PYEOF
Pass the above (without the surrounding code fence) as the command
argument to run_shell.
| Situation | Recommended tool |
|---|---|
| Small file (< ~2 KB) | write_file |
| Medium file, no errors yet | write_file (try first) |
Large file or write_file failed | run_shell + Python heredoc |
shell_agent also fails on large inline content | run_shell + Python heredoc |
base64 decoding inside the
Python script.EOF, PYEOF, FILEEOF) can be any string not
present as a standalone line in your content — choose accordingly.echo or printf approach.