Skip to content
C.W.K.
Stream
Lesson 01 of 04 · published

The Brief That Starved Its Own Instruction

~13 min · llm, context, bugs, architecture

Level 0Unsorted
0 XP0/36 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

The Output Noticed Before the Monitoring Did

A scheduled daily brief asks a model to survey the day: what the shelves carried, what a couple of opened articles said in depth, and what the temperature looked like on a social network. Three clauses, three corpus sections.

One morning the brief itself mentioned, in passing, that there were no temperature samples in what it had been given. The samples had been collected. The statistics said four of them. They were not in the corpus. The generated text was the first thing in the entire system to report the defect — which tells you exactly how much the monitoring was worth.

Growth Somewhere Else Broke It

The assembly was ordinary: build the headline section, then the extracts, then the samples, join, and cut to a character cap. That worked while the reader had a handful of shelves. Then the shelves roughly doubled, and their headlines alone exceeded the entire cap.

Nothing about the sample-gathering code changed. Nothing failed. The sections appended last simply fell off the end, silently, because they were appended last — and the sections appended last were the small unique ones, precisely because bulky sections feel like the main event and get written first. Tail truncation grants priority in code-authoring order, which correlates with nothing.

Reserve First, Fill What Remains

The repair inverts the priority. Build the small irreplaceable sections and measure them. Subtract from the cap. Whatever is left is the headline allowance, and the headlines fill it explicitly, stopping when the allowance is exhausted rather than being cut afterwards.

The distinction is worth stating precisely, because both versions produce a corpus under the cap. In the truncating version, the cap decides which sections exist. In the budgeted version, the author decides which sections exist and the cap decides how much breadth the bulky one gets. Same limit, opposite semantics.

Keep the Belt-and-Braces Cut, and Label It

A final absolute truncation still belongs at the end — budgets have arithmetic bugs, and exceeding a hard context limit is a failure worse than a shortened corpus. The difference is that it should now be unreachable in normal operation, and when it does fire it appends a visible marker rather than ending mid-sentence. A truncation that announces itself is a diagnostic; a silent one is the bug this whole lesson is about.

When context is scarce, allocation is a design decision — so make it one. Any assembly that ends in "and then cut to fit" has delegated that decision to the order somebody happened to write the code in, and will fail first on whichever section is smallest, most unique, and most explicitly required.

Code

Reserve the irreplaceable, budget the bulky, and label the last-resort cut·python
# BEFORE: append, then cut. The cap decides which sections exist.
corpus = header + shelves_section + extracts_section + samples_section
corpus = corpus[:CORPUS_MAX_CHARS]        # <- extracts and samples die here


# AFTER: reserve the small, irreplaceable sections; the bulky one
# fills what remains, round-robin, and stops on its own.
def build_corpus(con) -> tuple[str, dict]:
    stats = {"tabs": 0, "articles": 0, "extracts": 0, "samples": 0}

    # Small, unique, and named by clauses of the instruction -> reserved.
    samples_section = _section("X temperature", _sample_lines(con))
    extracts_section = _section("Opened in depth", _extract_lines(con))
    previous = _section("Yesterday's brief", _prior_brief(con))

    reserved = (len(CORPUS_HEADER) + len(samples_section)
                + len(extracts_section) + len(previous))
    shelf_budget = CORPUS_MAX_CHARS - reserved

    # Bulky and repetitive -> gets the remainder, ROUND-ROBIN so that
    # running out costs depth uniformly instead of erasing whole topics.
    lines, spent, dropped = [], 0, 0
    shelves = [list(s) for s in _shelf_headlines(con)]
    for rank in range(max((len(s) for s in shelves), default=0)):
        for shelf in shelves:
            if rank >= len(shelf):
                continue
            cost = len(shelf[rank]) + 1
            if spent + cost > shelf_budget:
                dropped += 1
                continue
            lines.append(shelf[rank])
            spent += cost
            stats["articles"] += 1

    stats["headlines_dropped"] = dropped      # disclosed, not hidden
    corpus = CORPUS_HEADER + "\n".join([*lines, extracts_section,
                                        samples_section, previous])
    if len(corpus) > CORPUS_MAX_CHARS:        # belt and braces, and LABELLED
        corpus = corpus[:CORPUS_MAX_CHARS] + "\n[corpus truncated]"
    return corpus, stats

External links

Exercise

Find a place in your systems where content is assembled and then truncated to a limit — a prompt, a log line, a notification, an email digest. Work out which section would disappear first, then check whether anything downstream depends on that section being present. Finally, make the assembly report how much it dropped.
Hint
The section that disappears first is the one appended last, which is usually the one added most recently — and recently-added sections are the ones some other code most likely depends on, because they were added to satisfy a requirement someone had just discovered.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.