Instruction Template Processing

Overview

Instruction Template Processing is a core mechanism that dynamically injects session state and artifact data into large language model (LLM) prompts at runtime. This capability enables agents to generate contextually rich instructions by substituting placeholders in templates with the current session’s state values or the contents of stored artifacts. The module addresses the problem of making LLM instructions adaptive to evolving session information and external data, thus enhancing the relevance and precision of AI agent interactions.

By processing instruction templates with dynamic data injection, the system decouples instruction authoring from runtime data availability, allowing template reuse and flexible prompt customization without manual intervention.


Core Concepts

Instruction Templates

Instruction templates are textual prompts or instructions containing placeholders enclosed in curly braces {}. These placeholders represent variables or artifacts that should be replaced by actual values from the session state or artifact storage at invocation time.

Example Template:

Hello {user_name}, your current task status is {app:task_status}.
Please refer to the document: {artifact.project_overview}.

Here, {user_name} and {app:task_status} are session state variables, while {artifact.project_overview} refers to the contents of an artifact named project_overview.

Placeholder Syntax and Semantics


Functionality and Workflow

At a high level, the Instruction Template Processing module performs the following:

  1. Parsing: Scans instruction text for placeholders matching the pattern {...}.

  2. Resolution: For each placeholder:

    • Determines whether it refers to a session state variable or an artifact.

    • Validates the variable name or artifact identifier.

    • Retrieves the corresponding value from the session state or artifact service.

    • Applies optional logic for missing data.

  3. Replacement: Substitutes placeholders with their resolved string values.

  4. Integration: Appends the processed instruction text to the outgoing LLM request payload.

This workflow is triggered automatically during LLM request construction, ensuring that instructions reflect the latest session and artifact data.


Interaction with System Components

The module is tightly coupled with the agent’s invocation lifecycle: when preparing an LLM request, it enriches instructions by injecting dynamic data, as seen in the instructionsRequestProcessor function which combines global and agent-specific instructions.


Key Files and Their Roles

internal/llminternal/instruction_processor.go

This file contains the main implementation. Its responsibilities include:

Example snippet illustrating template processing:

func InjectSessionState(ctx agent.InvocationContext, template string) (string, error) {
	// Find all {placeholders} and replace with session/artifact values.
	for each placeholder in template {
		replacement, err := replaceMatch(ctx, placeholder)
		if err != nil {
			return "", err
		}
		replace placeholder with replacement
	}
	return processedTemplate, nil
}

util/instructionutil/instruction.go

Provides a public-facing wrapper around the internal processing function, ensuring that only a read-only context is used when injecting session state into instruction templates. This enforces immutability guarantees during template injection.

internal/llminternal/instruction_processor_test.go

Contains comprehensive unit tests for instruction template processing, verifying scenarios including:


Instruction Template Processing Flow

flowchart TD
Start[Start: Receive Instruction Template]
Parse[Parse Template for Placeholders]
ForEach[For Each Placeholder]
IsArtifact{Is Placeholder an Artifact?}
LoadArtifact[Load Artifact Content]
GetState[Get Session State Value]
IsOptional{Is Placeholder Optional?}
Replace[Replace Placeholder with Value]
Error{Error?}
SkipOrFail[Skip or Fail Based on Optional Flag]
End[Return Processed Instruction String]
Start --> Parse --> ForEach
ForEach --> IsArtifact
IsArtifact -->|Yes| LoadArtifact
IsArtifact -->|No| GetState
LoadArtifact --> Error
GetState --> Error
Error -->|Yes| SkipOrFail
Error -->|No| Replace
SkipOrFail --> ForEach
Replace --> ForEach
ForEach -->|No More Placeholders| End

Design Patterns and Approaches


Relationship to Other Topics

For more detailed understanding of session state handling, see Session State Injection.


The Instruction Template Processing module thus enables powerful and dynamic prompt construction by integrating live session and artifact data, facilitating context-aware AI agent interactions.