
Structured Verbosity: The 'Key' to Savings
Learn how to optimize the content of your JSON/YAML fields. Master the 'Technical Shorthand' and 'Mapped Constants' patterns.
Structured Verbosity: The 'Key' to Savings
Even if your output is "Structured" (JSON), it can still be Verbose.
{"analysis": "I have looked at the document and I think it is great."}
This is "Narrative hidden in a Structure." You are still paying for the conversational fluff; you just wrapped it in curly braces. To achieve true token efficiency, we must optimize the Internal Content of our structured fields.
In this final lesson of Module 13, we learn how use Mapped Constants, Enum-Based Logic, and Symbolic Shorthand to turn "Chatty Data" into "Dense Data."
1. Mapped Constants (The 'ID' Pattern)
If your agent is classifying sentiment, don't have it output "POSITIVE" or "NEGATIVE."
The Efficiency Pattern:
- Assign
1to Positive,2to Neutral,3to Negative. - In your system prompt, say: "Output 1 for Positive..."
- Savings: 1 token vs 5-7 tokens.
Scaling the IDs
In a massive review-classification system (1 million reviews), move from "Word results" to "Integer results" saves 4 Million Tokens.
2. Using Enums for Constraint
Using Enums in your Pydantic model (Module 13.3) is more than just a coding best practice. It "Clamps" the model's vocabulary.
Wait, why?
An LLM works by predicting the next token. If the Enum only contains A, B, C, the model's energy is focused on choosing one of those three. It literally cannot "Wander" into a long explanation because the next expected tokens (based on the Schema) are short.
3. Implementation: The Constant Mapper (Python)
Python Code: Mapping Integers to Meaning
from enum import IntEnum
class Sentiment(IntEnum):
POSITIVE = 1
NEUTRAL = 2
NEGATIVE = 3
# In your processing code
raw_id = result['s'] # e.g. 1
sentiment_obj = Sentiment(raw_id)
print(f"Logic Result: {sentiment_obj.name}") # "POSITIVE"
4. Pruning the "Rationale" Field
Often, developers include a "rationale" or "explanation" field in their JSON for debugging.
Optimization: Only generate this field if Confidence is Low or if the app is in Developer Mode.
Dynamic Schema Strategy:
"If confidence greater than 0.9, output ONLY
{"id": X}. If confidence less than 0.9, output{"id": X, "reason": "..."}."
This ensures that the "Easy" cases (the 80%) are ultra-cheap, while the "Hard" cases (the 20%) get the reasoning they need.
5. Summary and Key Takeaways
- Integer Mapping: Use
1instead ofSUCCESSto save 5 tokens per response. - Enum Enforcement: Limit the model's choice to prevent "Vocabulary Drift."
- Conditional Rationale: Only ask for "Explanations" when the task is difficult.
- Binary Decision first: Map complex concepts to binary values (
true/false) whenever possible.
Exercise: The Mapping Challenge
- You are building an agent that categorizes "Customer Support Tickets" into 10 categories.
- Version A: Request the full category name (e.g. "Billing and Payments").
- Version B: Assign each category an ID (0-9).
- Compare the output tokens. (B is usually 5x smaller).
- Analyze: Did the Accuracy change?
- (Most students find Version B is More Accurate because the model has a clearer target).