Type-Safe Context Engineering: Lessons from Protein Folding
A formal type-theoretic framework for understanding AI prompt engineering, drawing parallels between protein folding and context design to build more reliable AI systems.
Type-Safe Context Engineering
Lessons from Protein Folding for Building Reliable AI Systems
Download: PDF
The Central Insight
Just as protein sequences uniquely determine native structures under Anfinsen's thermodynamic hypothesis, well-designed context should uniquely constrain model responses.
This insight from computational biology provides a principled framework for understanding when AI prompting succeeds and when it fails.
Context as Type
In type theory, a type specifies the set of valid values. A term is a value that inhabits a type.
We can view context engineering through this lens:
| Type Theory | Context Engineering |
|---|---|
| Type | Context (prompt, examples, constraints) |
| Term | Response |
| Type inhabitation | Valid response generation |
| Type error | Invalid/unexpected response |
Type-Safe Context
Context is type-safe when it uniquely determines the class of valid responses:
# Type-safe: constraints fully specify valid response structure
context = """
Return a JSON object with exactly these fields:
- "approved": boolean
- "issues": list of strings
- "summary": string under 100 chars
Review this code: {code}
"""
Type-Unsafe Context
Context is type-unsafe when it's ambiguous or underdetermined:
# Type-unsafe: many valid interpretations
context = "Review the code"
# Could mean: security review, style review, performance review...
The Protein Folding Analogy
Anfinsen's Dogma
Christian Anfinsen won the 1972 Nobel Prize for demonstrating that a protein's amino acid sequence uniquely determines its 3D structure. The sequence is the "type" and the structure is the "canonical term."
AlphaFold's Success
AlphaFold solved protein folding not by learning physics, but by learning efficient proof search over the constraint space defined by sequences. It succeeds because protein folding is type-safe—the sequence fully determines the structure.
Formal Framework
Definitions
Context Type (Γ): A specification that constrains the space of valid responses. Formally, Γ defines a set of constraints {c₁, c₂, ..., cₙ} that any valid response must satisfy.
Response Term (t): A model output. We write t : Γ to indicate that t satisfies all constraints in Γ.
Type Safety: A context Γ is type-safe if there exists a unique equivalence class of responses [t] such that for all valid responses t₁, t₂ : Γ, we have t₁ ~ t₂ under semantic equivalence.
The Typing Judgment
We define a typing judgment for context engineering:
Γ ⊢ t : T
Read as: "Under context Γ, response t has type T."
Type Constructors
| Constructor | Meaning | Example |
|---|---|---|
| A × B | Product (and) | JSON with fields A and B |
| A + B | Sum (or) | Either format A or B |
| A → B | Function | Given input A, produce B |
| Πₓ:A B(x) | Dependent | Output type depends on input |
Failure Mode Taxonomy
1. Underdetermined (Type Too Broad)
Context doesn't constrain the response space sufficiently:
# Problem: ambiguous
prompt = "Write code"
# Fix: specify language, purpose, constraints
prompt = """
Write a Python function that:
- Takes a list of integers
- Returns the sum of even numbers
- Handles empty lists by returning 0
"""
2. Overdetermined (Contradictory Constraints)
Context contains conflicting requirements—no valid response can exist:
# Problem: contradictory
prompt = """
Write a response that is:
- Under 50 words
- Covers all aspects of the topic in detail
"""
3. Type Mismatch
Response satisfies a different type than expected:
# Problem: format mismatch
prompt = "Analyze this data and return your findings"
# Fix: specify output type
prompt = """
Analyze this data and return a JSON object:
{
"findings": [string],
"recommendations": [string],
"confidence": float 0-1
}
"""
Chaperone Systems
Inspired by molecular chaperones that guide protein folding:
Retry Chaperone
def retry_chaperone(prompt, target_type, max_attempts=3):
for attempt in range(max_attempts):
response = model.generate(prompt)
try:
return target_type.model_validate_json(response)
except ValidationError as e:
prompt = f"{prompt}\n\nPrevious attempt failed: {e}\nTry again:"
raise TypingError("Could not inhabit type")
Progressive Refinement
def progressive_chaperone(task, target_type):
# Start with broad type
outline = model.generate(f"Outline: {task}")
# Refine to specific type
details = model.generate(
f"Given outline:\n{outline}\n\nNow provide: {target_type.schema()}"
)
return target_type.model_validate_json(details)
ContextFS Implementation
ContextFS implements these theoretical foundations through a formal type system.
Type Grammar (Definition 5.1)
BaseType ::= String | Int | Float | Bool | DateTime | UUID
MemoryType ::= Mem Schema -- Schema-indexed memory
VersionedType ::= Versioned MemoryType -- Timeline with ChangeReason
Mem[S] - Type-Safe Memory
from contextfs.types import Mem
from contextfs.schemas import DecisionData
# Type-safe access to structured_data
typed: Mem[DecisionData] = memory.as_typed(DecisionData)
print(typed.data.decision) # IDE knows this is str
VersionedMem[S] - Evolution Tracking
Memory evolution follows four formal change reasons:
| ChangeReason | Type-Theoretic Meaning |
|---|---|
OBSERVATION | New axiom added to context |
INFERENCE | Derived theorem from premises |
CORRECTION | Contradiction resolution |
DECAY | Axiom confidence reduction |
Design Principles
1. Explicit Type Signatures
Always specify the expected output structure using Pydantic or JSON Schema.
2. Constraint Completeness
Ensure constraints fully determine valid responses.
3. Progressive Refinement
Start with broad types and refine through multiple steps.
4. Chaperone Systems
Use validation and retry mechanisms for reliability.
Implications for AI Safety
Alignment as Type Safety
If we could specify human values as a type V, alignment becomes ensuring all actions inhabit the "aligned" type.
Robustness as Type Preservation
System is robust if type safety holds under perturbation of the context.
Interpretability as Type Inference
Understanding a model's behavior = inferring what type constraints it has learned.
References
- Anfinsen, C. B. (1973). Principles that govern the folding of protein chains. Science.
- Jumper, J., et al. (2021). Highly accurate protein structure prediction with AlphaFold. Nature.
- Wadler, P. (2015). Propositions as Types. Communications of the ACM.
- Martin-Löf, P. (1984). Intuitionistic Type Theory.
- Long, M. & YonedaAI Collaboration. (2025). Type-Safe Context Engineering. Preprint.