βοΈ Gemma 4 Tri-Craft Harness (v9.0 β Unified Synthesis Edition)
Repository:
OliviaRossi/Gemma-4-Chat-Template-Tri-Craft-Harness
Target Models: Google Gemma 4 (2B, 9B, 27B, 34B, and derivative instruction-tuned checkpoints).
The Tri-Craft Harness (v9.0) is an industrial-grade Jinja2/Minja prompt virtualization harness designed to steer Google Gemma 4 models simultaneously across three high-cognitive-demand disciplines:
$$\text{Artifact} = \underbrace{\text{Agentic Software Engineering}}{\text{Skeleton (Structure & Boundaries)}} + \underbrace{\text{Sensory UI/UX Design}}{\text{Body (Tokens & Ergonomics)}} + \underbrace{\text{Living Literary Prose}}_{\text{Soul (Microcopy & Clarity)}}$$
By refactoring prompt assembly into deterministic invariant heads and surface-aware craft gravity, v9.0 eliminates the two most pervasive failure modes in autonomous model generation: tool-loop fidelity collapse and prefix cache invalidation.
β‘ What Makes v9.0 Definitive?
| Challenge | Generic / Vendor Templates | Tri-Craft Harness v9.0 |
|---|---|---|
| Prefix Caching (TTFT) | Dynamic dates, timestamps, or workspace blocks disrupt the token sequence, causing complete cache misses across runs. | Topological Cache Invariance: Invariant system instructions sit at the absolute prefix head. Volatile elements are grouped strictly at the downstream tail. |
| Multi-Turn Tool Loops | Models suffer from "fidelity collapse" by turn 3βdropping CSS tokens, omitting edge states, and outputting bare unstyled JSON or script stubs. | Anti-Degradation Protocol: Strictly enforces identical UI styling, empathetic microcopy, and defensive architectural boundaries across all turns. |
| Medium Bleed | Asking for a bash script outputs unnecessary HTML/CSS; asking for a backend API outputs unsolicited prose. | Craft Gravity Adaptation: Contextually re-allocates discipline weight across Web/Native UI, CLI/Terminal, Backend APIs, and Technical Docs. |
| Schema Serializer | Serialization fails on nested $defs, tuple arrays (prefixItems), patternProperties, or scalar enums. |
Next-Gen JSON Schema Engine: Recursive dereferencing up to depth 8 with type-safe formatting without quote explosion. |
| Thought Channel Control | Uncontrolled reasoning traces waffle indefinitely or fail to consider user-facing aesthetic and accessibility needs. | Tri-Tier Cognitive Protocol: Formalizes `< |
ποΈ The 5 Core Architectural Pillars
1. Topological KV-Cache Prefix Invariance
High-throughput inference frameworks (vLLM Automatic Prefix Caching, SGLang RadixAttention, and llama.cpp --cache-prompt) rely on strict byte-for-byte token prefix matching.
In v9.0, the system turn is partitioned into two distinct physical zones:
- Static Invariant Head: All operational rules, craft gravity definitions, engineering standards, UI tokens, prose guidelines, and cognitive pacing templates. This segment produces an identical token hash across all sessions using the same mode.
- Volatile Downstream Tail: Dynamic engine clocks (
Current date: ...),<workspace>project states,<context>lore/style blocks, and leading user/developer messages.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STATIC INVARIANT HEAD (100% Shared Across Sessions -> CACHE HIT) β
β <|turn>system β
β <|think|> [Reasoning Blueprint] β
β [Operational Foundation + Gravity Rules + Tri-Craft Modules] β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β VOLATILE TAIL (Dynamic -> Computed On-Demand) β
β [Engine Clock / Current Date] β
β <workspace> [Authoritative State & Tokens] </workspace> β
β <context> [World Canon / Persona / Style Guide] </context> β
β [User System Overrides] β
β <|tool> [Tool Declarations] <tool|> β
β <turn|> β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2. Craft Gravity & Surface Specialization
Tri-craft mode (mode="tri") does not mean blindly dumping UI components into every prompt. The harness applies situational gravity based on the execution surface:
- Web & Native UI: Design leads. Enforces 8-state coverage (default, hover, focus-visible, active, disabled, loading, empty, error), OKLCH/semantic tokens, clamp() typography, semantic HTML5, and human-first microcopy.
- CLI & Terminal Tools: Code leads. Design manifests as clean ANSI color palettes, rigid column alignment, visual density, and clean stderr routing. Writing manifests as POSIX-compliant flags and high-signal diagnostics.
- Backend Systems & APIs: Code leads. Design manifests as intuitive domain modeling, predictable REST/RPC payloads, and structured logging envelopes. Writing manifests as actionable error contracts that explain the root cause and remediation.
- Documentation & Technical Guidance: Writing leads. Design manifests as scannable typographic hierarchy, visual tables, and comparative callouts. Code manifests as tested, copy-pasteable snippets.
3. Multi-Turn Tool Anti-Degradation Protocol
During extended tool execution loops (turns 3+), instruction-tuned models frequently degrade into minimal functional agents. They drop visual styling, strip out error boundaries, and emit generic placeholder microcopy ("Submit", "Success", "Error occurred").
The v9.0 harness injects persistent agentic invariants:
- Zero Placeholders: Forbids
// TODO,... rest of code unchanged, and placeholder text. - Persistent Polish: Intermediate and final outputs maintain the identical visual and architectural polish of turn 1, even when recovering from low-level compiler errors or unit test failures.
4. Next-Gen JSON Schema Serializer
The format_schema macro handles advanced JSON Schema (Draft 2020-12) specifications often emitted by modern OpenAPI/Pydantic specs:
- Tuple Array Validation: Supports
prefixItems:[...]for fixed-length positional arrays. - Pattern & Additional Constraints: Correctly renders
patternPropertiesandadditionalProperties: false. - Deep Reference Dereferencing: Resolves
$defsanddefinitionsup to 8 levels deep with cycle-detection guards. - Type-Aware Enums: Preserves literal booleans and numbers without spurious string quotation (
enum:[true, 1, "test"]).
5. Tri-Tier Cognitive Pacing (<|channel>thought)
When thinking is enabled (thinking_effort="medium"|"high"|"max"), the model must structure its internal monologue across three deliberate stages:
[Triage & Craft Gravity]: Identifies the primary surface medium, determines leading vs. supporting disciplines, defines the visual archetype, and selects the voice register.[Architecture & Tool Sequence]: Maps component structures, 8-state transition matrices, runtime boundaries, and plans parallel reads vs. sequential mutations.[Fidelity & Slop Audit]: Confirms zero placeholder code, verifies keyboard/WCAG accessibility, and purges formulaic AI buzzwords ("tapestry", "delve", "beacon", "testament", "symphony", "revolutionize").
π Engine Deployment & Configuration
A. Hugging Face transformers
Save the template file as chat_template.jinja or assign it directly to your tokenizer:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-27b-it")
# Load the v9.0 Tri-Craft Template
with open("chat_template.jinja", "r", encoding="utf-8") as f:
tokenizer.chat_template = f.read()
messages = [
{
"role": "user",
"content": "Create a high-density, real-time metrics console for an autonomous drone fleet."
}
]
# Render prompt with Tri-Craft kwargs
rendered_prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
chat_template_kwargs={
"mode": "tri",
"thinking_effort": "high",
"design_tokens": {
"surface_primary": "#0D1117",
"accent_status": "oklch(0.72 0.19 145)",
"font_data": "JetBrains Mono"
}
}
)
B. vLLM (OpenAI-Compatible Server)
Launch vLLM with Automatic Prefix Caching (APC) enabled to exploit the invariant static head:
vllm serve google/gemma-4-27b-it \
--chat-template ./chat_template.jinja \
--enable-prefix-caching \
--max-model-len 32768 \
--gpu-memory-utilization 0.90 \
--port 8000
API Request with Tool Calling & Thinking:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
response = client.chat.completions.create(
model="google/gemma-4-27b-it",
messages=[
{"role": "user", "content": "Deploy the latest staging migration and check health."}
],
tools=[
{
"type": "function",
"function": {
"name": "run_migration",
"description": "Executes pending database migrations.",
"parameters": {
"type": "object",
"properties": {
"target_env": {"type": "string", "enum": ["staging", "production"]}
},
"required": ["target_env"]
}
}
}
],
extra_body={
"chat_template_kwargs": {
"mode": "code+professional",
"thinking_effort": "medium"
}
}
)
C. SGLang (RadixAttention Server)
SGLangβs RadixAttention matches tree nodes from token index 0. The invariant head turns the core directives into a static root node:
python3 -m sglang.launch_server \
--model-path google/gemma-4-27b-it \
--chat-template ./chat_template.jinja \
--enable-paged-radix-cache \
--context-length 32768 \
--port 30000
D. llama.cpp (llama-server)
Run locally with full prompt caching and slot reuse:
./llama-server \
-m ./gemma-4-27b-it.gguf \
--chat-template-file ./chat_template.jinja \
-c 32768 \
--cache-prompt \
--slot-save-path ./slots \
--cont-batching \
--port 8080
ποΈ Template Configuration Kwargs
Pass these kwargs inside your inference framework's chat_template_kwargs or API extra_body:
| Kwarg | Type | Default | Description |
|---|---|---|---|
mode |
string |
"tri" |
Active craft profile: tri, code, design, writing, roleplay, professional, hybrid, raw. Can combine using + (e.g. code+writing). |
enable_thinking |
boolean |
Engine dependent | Master switch for `< |
thinking_effort |
string |
"none" |
Reasoning intensity: none, minimal, low, medium, high, max. |
preserve_thinking |
boolean |
false |
When true, retains thought channels from previous turns in multi-turn history. |
workspace |
dict | str |
none |
Injected into <workspace> at the downstream tail. Authoritative runtime code/filesystem state. |
design_tokens |
dict | str |
none |
Injected into <workspace>. Authoritative design system tokens (colors, spacing, typography). |
lorebook |
dict | str |
none |
Injected into <context>. Canon, world state, or business rules. |
character_sheet |
dict | str |
none |
Injected into <context>. Sets persona constraints (automatically activates roleplay mode if mode is unset). |
style_guide |
dict | str |
none |
Injected into <context>. Overrides microcopy, brand voice, or commit standards. |
current_date |
string |
Engine clock | Override date in YYYY-MM-DD format. |
raw_tool_output |
boolean |
false |
When false, defangs Gemma control tokens (<|...|>) inside tool output to prevent prompt injection. |
tool_output_limit |
int |
0 (unlimited) |
Truncates tool output to $N$ characters using a 70% head / 30% tail ratio with an omission notice. |
system_directive |
string |
none |
Replaces the built-in system modules. Pass "off" to disable all default instructions. |
π‘ Wire Format Specification
This template enforces canonical Google Gemma 4 wire formatting with robust OpenAI compatibility bridges:
<|turn>system
<|think|>
[Pacing Blueprint]
[Static Invariant Directives]
Current date: YYYY-MM-DD.
<workspace>...</workspace>
<context>...</context>
<|tool>declaration:tool_name{description:<|"|>...<|"|>,parameters:{...}}<tool|>
<turn|>
<|turn>user
[User Message]<turn|>
<|turn>model
<|channel>thought
1. [Triage & Craft Gravity]: ...
2. [Architecture & Tool Sequence]: ...
3. [Fidelity & Slop Audit]: ...
<channel|>
[Call Synthesis or Narrative Output]
<|tool_call>call:tool_name{arg_key:<|"|>arg_val<|"|>}<tool_call|>
<|tool_response>response:tool_name{result:<|"|>...<|"|>}<tool_response|>
[Post-Tool Final Synthesis]
<turn|>
Gemma 4 Special Token Glossary
<|turn>/<turn|>: Delimits conversational participants (system,user,model).<|channel>/<channel|>: Namespaced thinking boundary (<|channel>thought\n...<channel|>).<|tool_call>/<tool_call|>: Model-emitted function calls (call:fn{args}).<|tool_response>/<tool_response|>: Inlined execution results (response:fn{data}).<|tool>/<tool|>: System-level tool declaration enclosure.<|think|>: System-level trigger for cognitive activation.<|"|>: Gemma's boundary delimiter for escaped strings, schema definitions, and parameters.<|image|>,<|audio|>,<|video|>: Canonical multimodal channel markers.
π License & Attribution
This harness is distributed under the Apache 2.0 License.
- Author: Olivia Rossi
- Architecture: Gemma 4 Tri-Craft Unified Master Harness (v9.0)
- Upstream Model Family: Google Gemma 4