Skip to slide
028 / 170

CH 03 · Building the Prompt · 05/12

Read as one page

Real code: the base prompt is compiled into the binary

The PromptBuilder you just built takes base_instructions as one string you pass in. Codex has to find that string, and it has to sit in front of the saved conversation. The fallback text is compiled into the program.

pub const BASE_INSTRUCTIONS: &str = include_str!("../prompt.md"); // [1]
const PERSONALITY_SECTION_HEADER: &str = "# Personality";
// ... (context-window and token-limit overrides on the model record)
    if let Some(base_instructions) = &config.base_instructions { // [2]
        let model_messages = model.model_messages.get_or_insert_default();
        model_messages.instructions_template = Some(base_instructions.clone());
    } else if config.personality == Some(Personality::None)
        && let Some(instructions_template) = model
            .model_messages
            .as_mut()
            .and_then(|messages| messages.instructions_template.as_mut())
    {
        *instructions_template = strip_personality_section(std::mem::take(instructions_template)); // [3]
    }
// ... (the fallback record used when the model slug is unknown)
fn local_model_messages() -> ModelMessages {
    ModelMessages {
        instructions_template: Some(BASE_INSTRUCTIONS.to_string()),
        ..Default::default()
    }
}

Source: openai/codex, codex-rs/models-manager/src/model_info.rs (Apache-2.0)

On the way into the request, that string becomes a conversation item and is inserted at the front of input.

        // These prompt-only items are rebuilt on every request. Hash their visible payloads
        // within the thread so retries and resumed sessions preserve their identity.
        let prefix_namespace = Uuid::new_v5(
            &Uuid::NAMESPACE_OID,
            self.state.thread_id.to_string().as_bytes(),
        );
        let mut prefix = Vec::new();
        // ... (on the responses-lite path, a tools item is pushed onto prefix first)
        if !prompt.base_instructions.text.is_empty() {
            let mut instructions = ContextualUserFragment::into(BaseInstructionsFragment(
                prompt.base_instructions.text.clone(),
            ));
            instructions.set_id(Some(ResponseItemId::with_suffix(
                "msg",
                Uuid::new_v5(&prefix_namespace, prompt.base_instructions.text.as_bytes()), // [4]
            )));
            prefix.push(instructions);
        }
        input.splice(0..0, prefix); // [5]

Source: openai/codex, codex-rs/core/src/client.rs (Apache-2.0)

What to notice

  1. [1] include_str! pastes prompt.md into the binary at compile time. An unknown model slug gets that string as its instructions_template inside local_model_messages.
  2. [2] If the user set config.base_instructions, that string replaces the template on the model record. Your PromptBuilder has one base_instructions string and nowhere to put a second source that overrides it.
  3. [3] With personality set to none, and no custom template, strip_personality_section deletes from a line that is exactly # Personality through the next h1, or through the end of the string when no later h1 exists. Catalog templates in models.json contain that h1, so they lose the section. The compiled prompt.md used for an unknown slug has ## Personality, and the same function leaves that file unchanged. Your builder always sends the whole string, so it cannot turn that voice off.
  4. [4] BaseInstructionsFragment wraps the string as a developer item (that role outranks user) and adds no tags around the text. The id is msg_ plus a version-5 UUID of the thread id and the exact instruction bytes. The same text keeps the same id across retries.
  5. [5] splice(0..0, prefix) inserts the prefix at the start of input. The base text is rebuilt on every request. When incremental tools are on, build_prompt passes an empty string, so this splice does not add a second copy. That mode records the base text in history instead.

Session startup picks the config override if one is set, otherwise the text saved on the conversation, otherwise render_model_instructions for the current model. With no config override, a resumed session keeps that saved text. Your PromptBuilder never chooses among a config override, a saved session, and a model template.

Read it yourself: Open prompt.md and confirm the heading is ## Personality. Then in session/mod.rs, confirm the config override wins over the saved conversation and the model template.

Finished this idea?

Your learning progress stays in this browser.