Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Last active May 26, 2025 18:06
Show Gist options
  • Save robinsloan/6d5f4247b5f2239872bea8391fd63016 to your computer and use it in GitHub Desktop.
Save robinsloan/6d5f4247b5f2239872bea8391fd63016 to your computer and use it in GitHub Desktop.
Claude revision script. More info here: https://www.robinsloan.com/lab/claude-revision/
require "json"
require "anthropic"
# note: store initial draft in draft.txt
anthropic = Anthropic::Client.new(
api_key: "your_key_goes_here"
)
run_key = Random.rand(1_000_000)
# note:
# claude-sonnet-4-20250514
# claude-opus-4-20250514
CLAUDE_VERSION = "claude-opus-4-20250514"
NUM_ITERATIONS = 5
NUM_ITERATIONS.times do |i|
puts("doing iteration #{i}")
story_file = i == 0 ? "draft.txt" : "draft_#{run_key}_v#{i}.txt"
story_text = File.read(story_file)
prompt_to_plan = <<~PROMPT
You are a helpful editor. Please read the following draft of a story and propose a concise plan for improvement -- 5-10 changes that the author could implement. These should be VERY precise, targeted edits -- the structure of the story is already good, so we just want to polish it. Some of changes could be simple line edits. You are working with an expert author, so be sure to propose changes at their level.
The story text follows. Please read it and reply with your concise plan.
#{story_text}
PROMPT
response = anthropic.messages.create(
max_tokens: 4096,
messages: [{role: "user", content: prompt_to_plan}],
model: CLAUDE_VERSION,
temperature: 1.0
)
plan_text = response.content[0][:text]
puts("Plan:")
puts(plan_text)
if i == 0
File.write("first_plan_#{run_key}.txt", plan_text)
end
prompt_to_write = <<~PROMPT
You are a short story writer, expert in prose style and story structure. Please read the following draft of your story and the accompanying plan for improvement, then implement that plan. Your goal is to improve the draft considerably, without changing the macro structure -- this is a line edit. Emphasize crisp, stylish prose and an absence of cliche -- you are aiming for the highest level of writing, absolutely world class, sharp and thoughtful and entertaining.
Here is the draft text:
#{story_text}
And here is the plan for improvement:
#{plan_text}
Consider carefully which proposed items to accept and which to reject. You should only accept the 3-5 best ones -- not all edits are good. The length of your revised story shouldn't greatly exceed the length of this draft.
Please respond with the revised story.
PROMPT
response = anthropic.messages.create(
max_tokens: 4096,
messages: [{role: "user", content: prompt_to_write}],
model: "claude-opus-4-20250514",
temperature: 1.0
)
new_story_text = response.content[0][:text]
puts("New story:")
puts(new_story_text)
File.write("draft_#{run_key}_v#{i + 1}.txt", new_story_text)
end
puts("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment