# Code block actions > Generated code gets read in one place and used in another. - Canonical: https://patterns.konigi.com/ai-assistants/code-block-actions - Group: Output shape - Level: implementation - Status: published - Updated: September 12, 2026 - Also called: copy button, run snippet, apply to file --- ## Problem A fenced block of code sits in the middle of an answer. Its whole purpose is to end up somewhere else, and getting it there by selecting text with a mouse picks up line numbers, drops the last character, or misses the top line entirely. ## Solution Attach a small control cluster to the block itself. Copy first, because copy is what nearly everyone does. Then whatever the surrounding product can honestly offer: a language label, a run control where there's somewhere to run it, and an apply control where there's a file to apply it to. Copy has to be exact. The clipboard write should carry the raw source with original indentation and no decoration, which means line numbers, the language label, and any diff markers have to be excluded from the copied text rather than merely styled differently. Selecting rendered code with a pointer is exactly the operation that picks up all three, which is why a copy control is worth having even though the text is right there. The language label earns its space by doing two jobs: it tells the reader what they're looking at, and it's the honest place to admit uncertainty when the model didn't specify. A block silently rendered as one language and highlighted wrong is a small, constant irritation. Apply is the action worth the most and the one carrying real consequence. Writing generated code into a file is a change to the viewer's work, and HAX guideline 16 puts the obligation before the action rather than after: name the file, show the change as a diff, and require an explicit confirmation. An apply control that writes silently is the fastest path to someone losing work they can't recover. Run has the same shape with a sharper edge. Executing generated code is the point at which a suggestion becomes an action with effects, and the questions of where it runs, with what access, and what happens on failure belong in the interface rather than in documentation. Streaming complicates all of this. A block still arriving is incomplete, and a copy control that appears at the opening fence hands over half a function. The controls should wait for the closing fence, and a block whose fence never arrives because generation was stopped needs to say so rather than presenting itself as complete. ## Use when Answers regularly contain code, configuration, or commands that are meant to be used rather than read. ## Don't use when The snippet is illustrative. A three-word inline example wants inline code styling, and hanging a control cluster off it adds chrome to something nobody will copy. ## Trade-offs A control row on every block adds visual weight to answers that are already dense, and an answer with six blocks carries six of them. Copy is nearly free; run and apply are expensive to build well and dangerous to build carelessly. Line numbers help discussion and actively hurt copying, so a product wanting both has to keep two representations. A run control implies the code was checked. Nothing in the pipeline guarantees that. ## Checklist - Does copy carry the raw source with no line numbers or markers? - Is indentation preserved exactly? - Does the control cluster wait until the block has finished streaming? - Is the language labelled, and is a guess shown as a guess? - Does apply name the target file and show a diff before writing? - Is an apply reversible in one step? - Where does a run control execute, with what access? - Is copy reachable from the keyboard? - Is there feedback confirming the copy happened? - What does an unterminated block from a stopped generation look like? ## Compare **ChatGPT** puts copy on every block and adds a canvas path for code meant to be worked on rather than lifted, which separates reading from editing. **Claude** promotes substantial code into an artifact with its own controls and version history, so the block in the transcript stays a reference rather than the working copy. **Cursor** makes apply the primary action and writes the change into the open file as a reviewable diff. It's the highest-consequence version of the pattern here, and the most gated. **Perplexity** keeps the cluster to copy alone. Code there is evidence in an answer—not work in progress. ## Related Artifact panel is where a block graduates once it becomes the thing being worked on. Response actions is the same idea at turn scale. Suggestion diff is what an apply control should show before writing. Structured output is the sibling problem for tables. Streaming response determines when these controls may appear. ## Related patterns - [Artifact panel](https://patterns.konigi.com/ai-assistants/artifact-panel) — The answer is a document or a program, and a chat transcript is the wrong container for either. - [Response actions](https://patterns.konigi.com/ai-assistants/response-actions) — Copying, rating, sharing and rerunning all attach to one message, and none of them can dominate it. - [Suggestion diff](https://patterns.konigi.com/ai-assistants/suggestion-diff) — The model rewrote a paragraph and the viewer has to see exactly what changed before agreeing to it. - [Structured output](https://patterns.konigi.com/ai-assistants/structured-output) — The answer is a comparison, and prose is the worst available shape for one. - [Streaming response](https://patterns.konigi.com/ai-assistants/streaming-response) — A model takes ten seconds to finish a thought, and ten seconds of spinner reads as broken. ## Sources - [MDN, Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) - [GitHub, Code suggestions in GitHub Copilot](https://docs.github.com/en/copilot/concepts/completions/code-suggestions) - [Microsoft HAX Toolkit, Guideline 16, Convey the consequences of user actions](https://www.microsoft.com/en-us/haxtoolkit/guideline/convey-the-consequences-of-user-actions/) --- Screenshots on patterns.konigi.com are reproduced for commentary and criticism. Product names and marks belong to their owners.