SGR — schemas first, logic second, UI last
Schema-Guided Reasoning (SGR): all projects built from schemas to logic, not the other way around. Schemas are the contract between AI agent, business logic, and UI.
Order: Define domain (typed schemas) → Then logic (services work with schemas) → UI last (displays schemas, doesn’t parse strings).
When an AI agent works on a project, it MUST:
- Read schemas first —
Models/,schemas/,types/before any work - Generate structured output through typed schemas, not strings
- Validate at boundaries — agent I/O goes through schema validation
- Document the domain — enums, value objects, aggregates as code, not comments
SGR is the technical implementation of DDD:
- Bounded Context → separate set of schemas
- Aggregate → root schema with nested ones
- Ubiquitous Language → field names and enums = language of business
- Value Objects → enums and typed wrappers
By stack: Pydantic (Python), Zod (TypeScript), SwiftData @Model (iOS), data class + kotlinx.serialization (Kotlin). See SGR in practice: openai-oxide (Rust client with typed responses), rust-code (structured agent loop).
Skill: /sgr in solo-factory — design and implement SGR pipelines (NextStep agent loop, analysis cascade, tool dispatch). 4 reference files incl. working 304-line demo.
Libraries: sgr-agent crate v0.6.1 (Rust core for SGR agents), sgr-agent-core (Python, 1K+ stars), openai-oxide (typed Rust client).
- dev-principles-summary — origin: SGR section
- context-engineering — agent reading schemas first = context engineering for code
- harness-engineering-summary — schemas as architectural constraints (component 2)
- rlhf-book — RLHF = training-time alignment, SGR = inference-time alignment. Complementary strategies for the same goal