Skip to content

Module 2: Output Optimization

Forcing the LLM to Act as a Serialization Engine

Section titled “Forcing the LLM to Act as a Serialization Engine”

If Chapter 1 was about controlling how the machine thinks, this chapter is about controlling how the machine speaks.

When integrating LLMs into automated CI/CD pipelines, backend APIs, or data extraction workflows, conversational filler is not just an annoyance—it is a system-crashing bug. Downstream systems expect strictly typed data structures. When an API returns Here is the JSON data you requested: \n { ... }, automated parsers fail.

This module details the exact frameworks required to ruthlessy strip all conversational latency and force the model to behave as a pure, deterministic serialization engine.


Language models are mathematically weighted to be conversational. By default, they will wrap structured data in Markdown code blocks (e.g., ```json) or append helpful context. This requires backend engineers to write brittle RegEx sanitization layers to clean the payload before passing it to JSON.parse().

To bypass this, you must explicitly inject the target schema into the prompt context and issue a “Raw Mode” directive that threatens system failure if Markdown is used.

  1. Define the Schema: Pass a minified JSON schema or TypeScript interface directly in the prompt.
  2. The Raw Directive: Explicitly forbid standard Markdown block formatting.
  3. The Escape Hatch: Provide an exact string to output if the data cannot fit the schema, preventing the LLM from mutating keys to make it fit.
<directive>
You are a pure JSON serialization engine.
You must output a strictly valid JSON object conforming EXACTLY to the schema provided below.
CRITICAL FORMATTING RULES:
1. Do NOT wrap the output in ```json or ``` code blocks.
2. Do NOT output any conversational text before or after the JSON.
3. If the input data lacks the necessary information to complete the schema, output EXACTLY the string "ERR_INSUFFICIENT_DATA" and halt.
</directive>
<target_schema>
{
"transaction_id": "string (UUID)",
"is_fraudulent": "boolean",
"confidence_score": "float (0.0 to 1.0)",
"flagged_reasons": ["array of strings"]
}
</target_schema>

2.2. Ruthless Filler Stripping (Structured Outputs)

Section titled “2.2. Ruthless Filler Stripping (Structured Outputs)”

Even with aggressive negative prompting (“Do not write anything else”), models occasionally slip and generate a token like “Sure!” or “Here:” before outputting the structured data. This happens because the probability of those tokens following a user request is historically massive in the model’s training data.

The classic trick was the Prefix Override: end the request with a partial assistant message containing {, forcing the reply to start inside the JSON. Claude 4.6 and later models reject this. A request whose last message is a prefilled assistant turn returns a 400 invalid_request_error.

Claude’s structured outputs constrain the reply to a JSON schema you supply, so filler, Markdown fences and missing keys become impossible rather than unlikely. In Python, define the schema as a Pydantic model and let the SDK convert it:

from anthropic import Anthropic, transform_schema
from pydantic import BaseModel, Field
class Entities(BaseModel):
people: list[str] = Field(description="Person names exactly as written")
systems: list[str] = Field(description="System or service names mentioned")
client = Anthropic()
response = client.messages.create(
model="claude-haiku-5-5",
max_tokens=1024,
system="You are a data extractor. Use only what the text states.",
messages=[{"role": "user", "content": "Extract the entities from these logs: [LOG DATA]"}],
output_config={"format": {"type": "json_schema", "schema": transform_schema(Entities)}},
)
if response.stop_reason in ("refusal", "max_tokens"):
raise RuntimeError(f"No usable output: {response.stop_reason}")
entities = Entities.model_validate_json(response.content[0].text)

Check stop_reason before validating. A refusal or a reply cut off by max_tokens can still come back, and you want to report it as what it is, not as a parsing error.


When utilizing LLMs for autonomous code generation or refactoring, models frequently truncate long files using comments like // ... rest of code remains the same to save compute tokens. Furthermore, they may ignore project-specific linter rules.

The Solution: Boundry Enforcement & Anti-Truncation

Section titled “The Solution: Boundry Enforcement & Anti-Truncation”

You must construct an execution environment that threatens the model against truncation and binds the output to specific filepaths and stylistic guidelines.

  1. Enforce Linters: Specify the exact style guide (e.g., PEP8, ESLint AirBnB).
  2. Ban Placeholders: Add explicit negative constraints against lazy generation.
  3. Require Complete Artifacts: Demand drop-in ready code files.
<execution_rules>
1. OUTPUT COMPLETE ARTIFACTS: You must rewrite the ENTIRE file provided in <source_file>.
2. NO TRUNCATION: You are strictly FORBIDDEN from using placeholders, truncations, or comments such as "// ... existing code" or "// implementation here".
3. LINTER COMPLIANCE: All TypeScript output must strictly adhere to the ESLint AirBnB style guide. Use single quotes and trailing commas.
4. FILE BOUNDARIES: Wrap your complete file output exactly like this:
--- BEGIN FILE: path/to/file.ts ---
[CODE GOES HERE]
--- END FILE ---
</execution_rules>