Skip to slide
033 / 170

CH 03 · Building the Prompt · 10/12

Read as one page

Read the source yourself

Five stops, in the order a request is assembled. Every link is pinned to the commit this chapter quotes.

  1. Codex base text. Find BASE_INSTRUCTIONS in model_info.rs, open prompt.md, and notice the personality heading is ## Personality. strip_personality_section only deletes a line that is exactly # Personality, so this file loses nothing. The h1 it does delete is in the catalog instructions_template strings in models.json.
  2. Project files. In agents_md.rs, find dirs.reverse. The byte budget is project_doc_max_bytes a little earlier in load_project_instructions. Then open config_toml.rs and confirm DEFAULT_PROJECT_DOC_MAX_BYTES is 32 * 1024.
  3. Environment. In environment.rs, find render_diff and write down which fields count as a change. The session that records those messages is record_context_updates_and_set_reference_context_item in session/mod.rs.
  4. pi's prompt and pi's tools. In system-prompt.ts, read buildSystemPromptSections. The preamble is plain text. Every other section is wrapped in a tag of the same name. <project_context> holds the AGENTS.md and CLAUDE.md files, and renderProjectContext wraps each file in <project_instructions>. <cwd> is the working directory with backslashes flipped to forward slashes. Then in openai-completions.ts, convertTools is the function from the tool-schema slide.
  5. OpenCode's prompt, chosen by model family. In optimize.ts, OpenAIPlugin returns the GPT text only when the model id contains gpt, and the Astra variant when it also contains astra. That plugin's mode is override, so the text replaces the default system prompt. AnthropicPlugin returns the Claude text when the id contains claude, and its mode is append, so the text is added after the default. Both plugins leave the prompt alone when the agent already has a system prompt of its own. SessionSystemPrompt.render fills ${OPENCODE_TOOL_GUIDANCE} from the tool names present (shell, write, edit). The environment block is a different mechanism: builtins.ts builds a <env> string (working directory, workspace root, whether this is a git repo, platform, and a note to prefer the app temp directory over /tmp). Its changed renderer says the environment is now the new value, and Instructions.diff skips any key whose hash did not change.

Exercise: clone pi at the commit in those links, open openai-completions.ts, and find the line that adds strict only when the provider supports it. The comment above that line tells you what always sending the field would do. Do not leave the edit in place.

Finished this idea?

Your learning progress stays in this browser.