All posts

Preparing a GitHub Thread for a Coding Agent: Four Techniques, No Model

codenib threads keeps the agent's own thread whole, folds failed logs, resolves mentions to places in the code, and stores events instead of documents. On 76 pull request takeovers that cut input tokens by 21% to 45%, and the agents found the files to change about as often.

A coding agent asked to take over a pull request starts by reading the thread: the description, the comments, the reviews, the checks, a log. With gh that is one tool call per piece, and every call sends the whole conversation so far back to the model. codenib threads, a preview in CodeNib 0.2.6, reads the thread once, by code, before the agent starts. On 76 pull request takeovers that cut input tokens by 21% with Codex (gpt-6.1-sol) and 45% with Claude Code (Claude Opus 5.5). The agents found the files to change about as often either way.

Fetching ahead of the agent is the easy part. This post is about the harder part, deciding what goes into the document. On pull requests, fetching ahead without choosing costs more than it saves.

Left: an agent reading a thread itself makes one tool call for the issue, its comments, the linked pull request, the reviews, the checks and a failed log, and each call carries the conversation so far: with six pieces of 1,000 tokens, 21,000 tokens are sent in all. Right: codenib threads prepares one document with status, coverage, the ask, code locations, failing CI, other open pull requests on the same code, linked threads and discussion, ending in a cursor, so a later read returns only what changed. On 76 pull request takeovers, input tokens against reading with gh fell 21% with Codex (gpt-6.1-sol) and 45% with Claude Code (Claude Opus 5.5). Over the life of an issue, a first context and updates took a median of 2,897 tokens against 21,103 for rereading the thread, on 151 issues.
Figure 1. An example with six pieces of 1,000 tokens each. Read one call at a time, the agent is sent 21,000 tokens, because every call carries the conversation so far. The figures in the bottom row are measured.
Open the full-size diagram

Two costs that pull apart

We gave agents the same thread in three ways: the agent reads it with gh, every event is pasted into the prompt unprocessed, or the prompt carries the prepared context.

On issues, pasting everything cost the same as the prepared context, within 2%, and both took about a quarter fewer input tokens than gh. Nothing in the pasted prompt was selected or shortened, so that saving is only the tool calls that no longer happen.

On pull requests, pasting everything was the most expensive of the three. With Claude Code the 76 tasks cost $17.49 pasted, $11.34 with gh and $8.37 with the prepared context. Check logs, bot comments and review comments, unprocessed, cost more than the calls they replace.

So preparing a thread means removing the calls and then choosing. The choosing is done by code, with no model: the same thread always gives the same document, and every line in it traces to the event it came from. Four techniques carry most of it. In the diagrams below, teal is what the document keeps, blue is what an agent is sent, and a dashed outline is what was left out.

1. Never cut the thread the agent is working on

The obvious way to meet a token budget is to shorten long posts. For the agent's own thread this backfires: an agent that sees an omission mark in the pull request it was asked to fix reads the pull request again with gh, and has then paid for both.

A worked example with a budget of 3,000 tokens. Left: three threads in the store with the length of each post in tokens: #812, the one the agent works on, 2,100 tokens in five posts; a linked issue #790, 1,570 in four; a linked pull request #801, 1,120 in three. Right: the context keeps all 2,100 tokens of #812. Status lines take 150, which leaves 750 for the linked threads. Newest posts are taken first: 250 and 300 from #790 and 190 from #801 fit, and the four older posts are left out.
Figure 2. A worked example at the default budget of 3,000 tokens. All 2,100 tokens of the agent's own thread are kept, and the 750 left over go to the newest posts of the linked threads. The real line for a Caddy pull request follows.
Open the full-size diagram

So the budget is not applied evenly, and the second line of the document tells the agent which case it is in. This is caddyserver/caddy#8024 on November 14, 2026, with […] where we left text out and the line wrapped to fit:

Coverage: this thread complete, linked threads partial.
Everything people wrote in #8024 is below in full; […]
Reading #8024 again with gh returns nothing more.

The last sentence is written to be acted on. A post omitted from a linked thread can be fetched by its id, and the thread itself needs no second read.

2. A failed log is mostly one line, repeated

A linter that finds one problem in 300 files prints 300 lines, and a build matrix fails the same way on six platforms. Keeping the last 200 lines of each failed log, as our pasted prompts did, fills the prompt with one message.

Left: a job log with four set-up lines and 300 error lines that differ only in the file, the line number and the name. Right: the context keeps the lines around an error, compares them with paths and numbers blanked, shows a repeated line twice and counts the rest: 298 more lines like the two above. Jobs whose failing lines mostly match are printed once with the names of the jobs that share the failure, and are never merged by name.
Figure 3. A made-up log with 300 lines of one error, folded by the rules the context uses: two lines are shown and 298 are counted.
Open the full-size diagram

Jobs are folded the same way as lines, by what failed and never by name: a matrix runs several jobs under one name, and merging by name could hide the one that failed. This is the Caddy pull request from above:

## Failing CI
Checks at 0b3db48f: 2 failing, 22 passing.
[#8024/check/127120839152] lint (linux) failed
[…]
##[error]modules/caddyhttp/reverseproxy/reverseproxy.go:658:24: use "Sec-Websocket-Key" instead of "Sec-WebSocket-Key" (canonicalheader)
[…]
[#8024/check/127120838129] test (windows, 1.27) failed
[…]
	github.com/klauspost/compress/zstd: github.com/klauspost/[email protected]: read "https://proxy.golang.org/github.com/klauspost/compress/@v/v1.20.1.zip": stream error: stream ID 1; INTERNAL_ERROR; received from peer
[... 1 more lines like the two above ...]

This pull request changes two files in Caddy's file server. The lint error is in reverseproxy.go, and the Windows job stopped while downloading a module. Neither failure is about the change, and the agent can see that without opening a log.

3. Turn mentions of code into places in the code

A thread talks about code in words. Someone pastes a stack trace, a reviewer comments on a line, someone writes a path or a function name in backticks. An agent reading that has to find each one in the repository before it can start, with a search and a file read for each. The context does that lookup ahead of time and lists the result: a file, a line range, and the function or type those lines belong to.

The lookup uses no model. Regular expressions find the mentions. Each is resolved against one commit of your clone, read through Git, so the same thread gives the same locations whatever is edited in the working tree. A line is mapped to the innermost function or type that contains it: a line number alone tells an agent little, and the function around it is something it can open.

Left: the five ways a thread points at code, strongest evidence first: a stack frame, a review comment on a line, a file path, a name in backticks, and a file a diff touches. Right: the mention is resolved at one commit of the clone. Line 212 of pkg/io.py lies in the method read, lines 190 to 229, inside the class Reader, and the context lists that range with the evidence and the event it came from. An open pull request that changes lines 205 to 214 falls in the same range, so the context lists it as also in progress.
Figure 4. A stack frame at line 212 of a made-up file resolves to the function around it, Reader.read(), lines 190 to 229. The real locations for the Caddy pull request follow below.
Open the full-size diagram

Mentions are not equally trustworthy, so the list runs from stack frames, which are almost always right, down to bare names, which could be many things. A bare name is looked up first in the files the thread already mentions, and one defined in more than three places is dropped as a guess. Each entry ends with its kind of evidence and the event that pointed at it:

## Code locations
[…]
- modules/caddyhttp/fileserver/browse.go:85-229 `FileServer.serveBrowse()` (path; #8023/comment/6594174437)

Line ranges also answer a question the thread never raises: is someone else changing this code right now? codenib threads sync --open-pulls records which lines each open pull request changes. When one of those ranges falls inside a function the thread points at, the context says so:

## Also in progress on this code
- #7777 (open PR by Jualhosting, updated 2026-10-26) fileserver: Improve 404 error detail and context — changes `FileServer.serveBrowse()`; also edits modules/caddyhttp/fileserver/staticfiles.go

An agent working one thread has no way to see this for itself, and it is common. In astral-sh/ruff, 30 of the 59 most recently updated open pull requests changed a symbol that another of them also changed.

In this preview the locations come only from what the thread says. They do not use CodeNib's code graph, so the context does not know what calls a function.

4. Store what happened, and build the document on demand

The obvious thing to keep for each thread is the document handed to the agent. We keep something else: the list of things that happened on GitHub. Every comment, review, commit and check run is saved as its own record, in the order it arrived, under a fixed address such as #8024/review_comment/4477879638. Records are only ever added. An edited comment is added again under the same address, and the old record stays. The document itself is not stored. It is built from the list each time someone asks.

The reason is the second visit. An agent that comes back the next day needs to know what changed. A stored document cannot answer that: you hand over the whole document again, or you compare two texts and guess. With a list the answer is a position, everything after the last record the agent saw. Three things follow.

Left: a thread stored as a list of six events in order, each with a fixed address; an edited comment is appended as a new record under its old address. Right: agents keep a position in that list. Agent A's first read is sent the four records that exist and ends with a cursor. When it comes back with --since it is sent only the edit and the check that came after. Agent B opens a ticket on the same stored thread and is sent all six; when it polls again nothing is new and nothing is sent. Measured: over the life of an issue, a first context and updates took a median of 2,897 tokens against 21,103 for rereading, on 151 issues; with 24 tickets following 71 threads of astral-sh/ruff, fetching everything took 278 requests and a poll with nothing new took 13.
Figure 5. Six stored records and two agents. Agent A is sent four records, then the two that followed; agent B is sent all six from the same store. The figures under the rule are measured.
Open the full-size diagram

Every line can be checked. The bracketed ids in the excerpts above are addresses, and codenib threads events <id> prints the raw event.

Coming back is cheap. The document ends with a cursor, the address of the newest record it used, and --since <cursor> returns only what came after it. Across 151 issues with a median of six returns each, a first context followed by updates took a median of 2,897 tokens over the life of the issue, against 21,103 for reading the thread again each time.

Many agents can share one store. A ticket is a piece of work that remembers its position in each list it follows. Two tickets on one thread store it once, and a comment edited after a ticket read it comes back as unread. A poll asks GitHub once which threads changed and fetches only those. With 24 tickets following 71 threads of astral-sh/ruff, fetching everything took 278 requests, and a poll with nothing new took 13.

The four on one pull request

The four techniques act on the same thread at once. Below is a made-up pull request, from its first record to a second agent joining. Each step shows the record that was added, what the document says after it, and what an agent is sent.

This walkthrough needs JavaScript. The four diagrams above show the same steps.

Teal marks what the last step changed in the document, blue what the last read sent to an agent. The pull request and its records are made up; the rules are the ones codenib threads applies.

Same answers, fewer tokens

Each task showed an agent a thread and the repository as they stood at a cut-off, and asked which files have to change. The answer was compared with what the merged pull request changed. Files found are listed for gh, pasted and the prepared context, in that order; token columns compare the prepared context with each of the other two.

Files found and input tokens with the prepared context, by agent and task
TakeoverTasksFiles foundTokens vs ghvs pasted
Codex, pull requestgpt-6.1-sol760.74 / 0.75 / 0.75−21%−32%
Codex, issuegpt-6.1-sol470.82 / 0.85 / 0.85−25%+2%
Claude Code, pull requestClaude Opus 5.5760.83 / 0.78 / 0.80−45%−69%

The answers hold while the cost falls. With Codex the files found are the same. With Claude Code they are close, 0.80 against 0.83, a gap too small to tell from chance on 76 tasks. Pasting the whole thread shows the same small gap (0.78), so it comes from handing context over and not from what the context leaves out: an agent given the thread up front reads a little less code. The document therefore ends by telling the agent that it covers the threads and not the code.

The saving grows with the thread: on the 22 pull requests whose threads ran over 20,000 tokens, input tokens fell 34% with Codex and 63% with Claude Code.

Pull request tasks come from astral-sh/ruff, caddyserver/caddy and pydantic/pydantic; issue tasks from the first two. Codex ran gpt-6.1-sol and Claude Code ran Claude Opus 5.5, both at medium effort. The result shows where an agent would work and what reaching that cost, not whether it then writes a better fix. Intervals, method and the script to reproduce it are in the thread context guide.

Try it

It needs CodeNib 0.2.6, Git, and either a GitHub token in GH_TOKEN or a signed-in gh. Run it inside a clone of the repository:

python -m pip install -U codenib

codenib threads context 812                        # write THREAD_CONTEXT.md before the agent starts
codenib threads show 812 --sync --since '<cursor>'  # later: only what changed
codenib threads open 812                           # or let a ticket remember what it read

Writing the file before the agent starts does not depend on the agent choosing to call a tool. For later reads, the CodeNib plugin for Claude Code and Grok Build adds /codenib:thread, and codenib threads mcp serves the same to any agent that speaks MCP.

CodeNib executes and follows nothing in a comment, but the text still reaches your agent, so give the agent no more permissions than you would when it reads GitHub directly. Every command and option is in the thread context guide.