0Pricing
AI Prompt Engineering · Lesson

Domain Glossary and Ontology Injection

Embedding domain-specific terminology and knowledge into system prompts.

Domain Glossary and Ontology Injection is a free AI Prompt Engineering lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Disambiguation Problem

Domain language is full of ambiguity. 'Yield' means bond yield in finance and crop yield in agriculture. 'Resolution' means screen resolution in UI and issue resolution in support. Without domain context, models default to the most common general-language meaning — which is wrong in specialized domains.

Glossary Injection Pattern

Inject a domain glossary directly into the system prompt. This overrides the model's default vocabulary and ensures domain-specific terms are interpreted correctly throughout the session.

FINANCE_GLOSSARY = '''
DOMAIN GLOSSARY (these definitions override general language meaning):
- yield: bond yield (annual return as percentage of bond price), NOT crop or harvest
- duration: interest rate sensitivity measure (modified duration), NOT time length
- spread: yield spread between two bonds, NOT physical spreading
- convexity: second-order price sensitivity to interest rate changes, NOT geometry
- tenor: remaining time to maturity of a financial instrument, NOT musical pitch
- floor: minimum interest rate in a rate agreement, NOT building floor
- cap: maximum interest rate, NOT a hat or market capitalization
- swap: exchange of cash flows between counterparties, NOT physical exchange
- basis: difference between spot and futures price, NOT foundation
'''

FINANCE_SYSTEM_PROMPT = (
    'You are a fixed income analyst.\n\n'
    + FINANCE_GLOSSARY +
    '\nAlways use these domain definitions when answering questions.'
)

import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')

response = client.messages.create(
    model='claude-opus-4-5', max_tokens=500,
    system=FINANCE_SYSTEM_PROMPT,
    messages=[{'role': 'user', 'content': 'What is the yield of a 10-year bond?'}]
)
print(response.content[0].text)

Building a Domain Glossary File

Store glossaries as structured YAML files so they can be versioned, shared across prompts, and updated by domain experts without touching prompt code.

# glossaries/fixed_income.yaml
glossary:
  yield:
    domain_meaning: Annual return on a bond as a percentage of its current market price
    general_meaning: Crop or harvest output
    use_domain: true
    examples:
      - 'The 10-year Treasury yield rose to 4.5%'
      - 'Current yield = annual coupon / market price'

  duration:
    domain_meaning: |
      Measure of a bond's price sensitivity to interest rate changes.
      Modified duration = -dP/P / dr
    general_meaning: Length of time
    use_domain: true

  basis:
    domain_meaning: Difference between spot price and futures price of the same instrument
    general_meaning: Foundation or base
    use_domain: true

# glossaries/load.py
import yaml

def load_glossary(domain):
    with open(f'glossaries/{domain}.yaml') as f:
        data = yaml.safe_load(f)
    lines = ['DOMAIN GLOSSARY:']
    for term, info in data['glossary'].items():
        lines.append(f'- {term}: {info["domain_meaning"].strip()}')
    return '\n'.join(lines)

Ontology Injection for Complex Domains

An ontology goes beyond a glossary — it defines relationships between concepts: hierarchies, constraints, and rules. Injecting an ontology helps the model understand which concepts belong to which categories and how they relate.

MEDICAL_ONTOLOGY_SNIPPET = '''
CLINICAL ONTOLOGY (use these relationships in all analysis):

Diagnosis Hierarchy:
- Condition > Category > Specific Diagnosis
- "Hypertension" is a specific diagnosis under "Cardiovascular Conditions"
- "Type 2 Diabetes" is under "Endocrine / Metabolic Conditions"

Medication Classes:
- ACE inhibitors (e.g., lisinopril) -> used for: hypertension, heart failure, CKD
- Beta-blockers (e.g., metoprolol) -> used for: hypertension, angina, heart failure
- Statins (e.g., atorvastatin) -> used for: hyperlipidemia, cardiovascular risk

Measurement Rules:
- "BP" means Blood Pressure, format: systolic/diastolic (e.g., 130/85 mmHg)
- "A1c" means glycated hemoglobin; > 6.5% is diagnostic for Type 2 Diabetes
- "eGFR" means estimated Glomerular Filtration Rate; < 60 mL/min/1.73m2 = CKD

Always use ICD-10 categories when classifying diagnoses.
'''

print(MEDICAL_ONTOLOGY_SNIPPET[:300])

Dynamic Glossary Generation

For large knowledge bases, generate a focused glossary dynamically — extract only the terms most relevant to the current task from a master glossary, keeping the context window lean.

import json

# master_glossary.json — full domain glossary
MASTER_GLOSSARY = {
    'yield': 'Bond yield: annual return as percentage of current market price',
    'duration': 'Modified duration: bond price sensitivity to rate changes',
    'convexity': 'Second-order rate sensitivity measure',
    'swap': 'Exchange of fixed and floating cash flows',
    'option': 'Contract giving right (not obligation) to buy/sell an asset',
    'beta': 'Stock volatility relative to market index',
    'alpha': 'Excess return over benchmark after adjusting for risk',
    # ... hundreds more
}

def focused_glossary(user_query, master_glossary, max_terms=10):
    '''Select glossary terms most relevant to the user query.'''
    query_lower = user_query.lower()
    relevant = {}
    for term, definition in master_glossary.items():
        if term.lower() in query_lower or any(
            word in query_lower for word in definition.lower().split()[:5]
        ):
            relevant[term] = definition
        if len(relevant) >= max_terms:
            break
    lines = ['RELEVANT DOMAIN TERMS:']
    for t, d in relevant.items():
        lines.append(f'- {t}: {d}')
    return '\n'.join(lines)

query = 'What is the duration and convexity of this bond portfolio?'
print(focused_glossary(query, MASTER_GLOSSARY))

Multi-Domain Disambiguation

Some queries span multiple domains. Inject domain context for all relevant domains and instruct the model to disambiguate based on the conversation context.

MULTI_DOMAIN_SYSTEM = '''
This system serves both agricultural and financial users.
The domain is determined by context cues in the user message.

Domain disambiguation rules:
- If the user mentions "crops", "harvest", "acres", "soil", "planting":
  Use AGRICULTURAL definitions: yield = crop output, spread = physical spreading
- If the user mentions "bonds", "portfolio", "maturity", "coupon", "treasuries":
  Use FINANCIAL definitions: yield = bond yield, spread = yield spread
- If the domain is ambiguous:
  Ask the user to clarify: "Are you asking about agricultural or financial yields?"

AGRICULTURAL GLOSSARY:
- yield: crop output per unit area (e.g., bushels per acre)
- basis: difference between local cash price and futures price for a commodity

FINANCIAL GLOSSARY:
- yield: annual bond return as percentage of current price
- basis: yield spread between two financial instruments
'''

print('Multi-domain system prompt loaded.')
print('The model will ask for clarification when domain is ambiguous.')

Ontology-Constrained Output

Ontology injection can constrain the model's output to use only pre-defined categories, preventing free-form categorization that breaks downstream processing.

SUPPORT_ONTOLOGY_SYSTEM = '''
You are a support ticket classifier for a B2B SaaS company.

TICKET CATEGORY ONTOLOGY (use ONLY these exact category names):
Level 1 Categories:
- Billing > Sub-categories: Invoice Error, Subscription Change, Refund Request, Payment Failure
- Technical > Sub-categories: Bug Report, Performance Issue, Integration Error, Feature Not Working
- Account > Sub-categories: Access Request, User Management, Security Concern, Password Reset
- Feature Request > Sub-categories: New Feature, Enhancement, UI/UX Improvement

CLASSIFICATION RULES:
1. Always return exactly one Level 1 category and one Sub-category.
2. If ticket spans multiple categories, choose the PRIMARY issue.
3. If uncertain, use the category that would route to the most qualified team.
4. Return format: {"category": "Technical", "subcategory": "Bug Report", "confidence": "HIGH"}
   Confidence: HIGH (clear), MEDIUM (likely), LOW (ambiguous)
'''

def classify_ticket(ticket_text):
    import json
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=100,
        system=SUPPORT_ONTOLOGY_SYSTEM,
        messages=[{'role': 'user', 'content': f'Classify: {ticket_text}'}]
    )
    return json.loads(response.content[0].text)

Legal Ontology Injection

Legal domain ontologies define contract clause hierarchies, party relationships, and obligation types. Injecting these ensures consistent classification across all contract analysis tasks.

LEGAL_ONTOLOGY = '''
CONTRACT CLAUSE ONTOLOGY:

Obligation Types:
- SHALL: mandatory obligation (enforceable duty)
- MAY: permissive right (optional action)
- SHALL NOT: mandatory prohibition
- WILL: future intention (weaker than SHALL)

Clause Risk Hierarchy:
- CRITICAL: financial exposure > $1M or termination rights
- HIGH: material business impact, IP rights, indemnification
- MEDIUM: operational restrictions, notice requirements
- LOW: administrative provisions, definitions

Party References (standardize to these canonical forms):
- "the Company", "we", "us" -> VENDOR
- "Customer", "Client", "you" -> CUSTOMER
- "third party", "subcontractor" -> THIRD_PARTY

Always use these canonical party names in your analysis.
Do not use the actual company names — replace with canonical form.
'''

print('Legal ontology loaded. Party names will be canonicalized in all analysis.')

Terminology Consistency Checker

After receiving model output, verify that domain terms are used consistently and not slipping back into general-language meanings. A post-processing check catches terminology drift.

PROHIBITED_GENERAL_MEANINGS = {
    # In fixed income context: these general meanings should not appear
    'yield': ['harvest', 'crop', 'produce', 'give way', 'surrender'],
    'duration': ['how long', 'length of time', 'period of time'],
    'floor': ['ground floor', 'building floor', 'floor plan'],
    'cap': ['hat', 'market cap', 'bottle cap'],
}

def check_terminology_consistency(text, domain_term):
    text_lower = text.lower()
    prohibited = PROHIBITED_GENERAL_MEANINGS.get(domain_term, [])
    violations = []
    for general_phrase in prohibited:
        if general_phrase in text_lower:
            # Find context window around the violation
            idx = text_lower.index(general_phrase)
            context = text[max(0, idx-50):idx+80]
            violations.append({'phrase': general_phrase, 'context': context})
    return violations

# Usage after LLM call
output = 'The yield of the bond is 4.5% per annum based on current market price.'
violations = check_terminology_consistency(output, 'yield')
if violations:
    print('Terminology violation detected:', violations)
else:
    print('Terminology consistency: PASS')

Glossary Version Management

Domain glossaries must be versioned alongside prompts. A terminology change (a new regulatory definition, an updated clinical standard) requires re-evaluation of all prompts that use the affected terms.

# Glossary versioning with impact tracking
GLOSSARY_VERSIONS = {
    '1.0.0': {
        'yield': 'Bond yield: annual coupon / face value (current yield)',
        'duration': 'Macaulay duration'
    },
    '2.0.0': {
        'yield': 'Bond yield: annual return as % of current market price (yield to maturity)',
        'duration': 'Modified duration (more precise for risk management)',
        'convexity': 'Second-order rate sensitivity (new in v2)'  # new term
    }
}

def get_affected_prompts(old_version, new_version, prompt_registry):
    '''Find prompts that use terms changed between glossary versions.'''
    old_terms = set(GLOSSARY_VERSIONS[old_version].keys())
    new_terms = set(GLOSSARY_VERSIONS[new_version].keys())
    changed_terms = old_terms ^ new_terms  # symmetric difference

    affected = []
    for prompt_id, artifact in prompt_registry.items():
        if any(term in artifact['template'] for term in changed_terms):
            affected.append(prompt_id)
    return affected

print('Prompts affected by glossary v1.0.0 -> v2.0.0 update:', ['rate-analysis-v1', 'bond-report'])

Hierarchical Ontology with Parent-Child Relationships

Full ontologies define parent-child concept hierarchies. Prompting with a hierarchy allows the model to reason at the right level of specificity — neither too broad nor too narrow.

PRODUCT_ONTOLOGY = '''
PRODUCT CATEGORY ONTOLOGY (use for all product classification tasks):

Electronics
  Computing
    Laptops
      Gaming Laptops
      Ultrabooks
      Workstations
    Desktops
    Tablets
  Consumer Electronics
    Smartphones
    Smart Speakers
    Wearables
      Smartwatches
      Fitness Trackers

CLASSIFICATION RULES:
1. Always classify to the most specific level where evidence exists.
2. If a product matches multiple branches, use the primary use case.
3. Use exact taxonomy names from above — do not invent new categories.
4. If a product does not fit, use the nearest parent category and
   add "[NON-STANDARD: <reason>]" after the category name.
'''

print('Product ontology ready. 4-level hierarchy loaded.')

Quick Check

A model is deployed to analyze bond portfolios. Without a glossary injection, the model interprets 'What is the yield on this instrument?' by describing crop output. What is the root cause and fix?

Glossary and Ontology Injection Summary

Domain glossary and ontology injection resolves terminological ambiguity at the system level:

  • Glossary injection: define domain-specific meanings in the system prompt for ambiguous terms
  • Ontology injection: provide concept hierarchies, relationship rules, and classification constraints
  • Dynamic glossary: select only relevant terms from a master glossary to keep context windows lean
  • Multi-domain disambiguation: inject rules for context-based domain detection
  • Versioning: glossaries must be versioned and prompts must be re-evaluated when terms change
  • Consistency checking: post-process outputs to detect terminology drift

Frequently asked questions

Is the “Domain Glossary and Ontology Injection” lesson free?

Yes — the full text of “Domain Glossary and Ontology Injection” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.

What will I learn in “Domain Glossary and Ontology Injection”?

Embedding domain-specific terminology and knowledge into system prompts. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Prompt Engineering?

No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Domain Glossary and Ontology Injection” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Prompt Engineering lesson?

Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Legal Domain Prompt Patterns
  2. Medical and Clinical Prompting
  3. Financial and Quantitative Prompts
  4. Domain Glossary and Ontology Injection
← Back to AI Prompt Engineering