Advanced

Domain-Specific Prompt Engineering

Lesson 4 of 4 Estimated Time 50 min

Domain-Specific Prompt Engineering

LLMs are general-purpose, but each domain—legal, medical, financial, scientific—has specialized vocabulary, regulatory requirements, and evaluation criteria. This lesson teaches you to adapt prompting for specific domains while maintaining accuracy and compliance.

Understanding Domain-Specific Challenges

Different domains require different approaches:

Legal Domain:
- Challenge: Precise language matters (e.g., "liable" vs "responsible")
- Requirement: Cite relevant statutes/precedents
- Risk: Giving legal advice vs. legal analysis
- Prompt adjustment: Strict terminology, citation format, disclaimer

Medical Domain:
- Challenge: Accuracy of clinical information critical
- Requirement: Evidence-based, recent data
- Risk: Incorrect medical advice causing harm
- Prompt adjustment: Clinical evidence focus, liability disclaimers, expert review

Financial Domain:
- Challenge: Data freshness, numerical precision
- Requirement: Market data, regulatory compliance
- Risk: Bad advice causes financial loss
- Prompt adjustment: Data sources, risk disclaimers, scenario analysis

Scientific Domain:
- Challenge: Reproducibility, current research
- Requirement: Peer review, methodology
- Risk: Outdated or incorrect scientific claims
- Prompt adjustment: Literature references, uncertainty quantification

Domain Vocabulary and Terminology

Specialized domains use precise terminology:

from dataclasses import dataclass
from typing import List, Dict

@dataclass
class DomainTerm:
    """Definition of a domain-specific term."""
    term: str
    definition: str
    synonyms: List[str]
    antonyms: List[str]
    example_usage: str

class DomainLexicon:
    """
    Maintain domain-specific vocabulary to improve prompt accuracy.
    """

    def __init__(self, domain: str):
        self.domain = domain
        self.terms: Dict[str, DomainTerm] = {}

    def add_term(self, term: DomainTerm):
        """Add a term to the lexicon."""
        self.terms[term.term.lower()] = term

    def build_terminology_context(self) -> str:
        """Build context string explaining key terms."""
        context = f"Key terminology for {self.domain}:\n\n"

        for term, definition in self.terms.items():
            context += f"- {term}: {definition.definition}\n"
            if definition.synonyms:
                context += f"  Synonyms: {', '.join(definition.synonyms)}\n"
            if definition.example_usage:
                context += f"  Example: {definition.example_usage}\n"
            context += "\n"

        return context

# Example: Legal domain lexicon
legal_lexicon = DomainLexicon("contract law")

legal_lexicon.add_term(DomainTerm(
    term="liable",
    definition="Legally responsible for damages or losses",
    synonyms=["responsible", "accountable"],
    antonyms=["not liable", "innocent"],
    example_usage="The defendant was found liable for breach of contract."
))

legal_lexicon.add_term(DomainTerm(
    term="breach",
    definition="Failure to perform obligations under a contract",
    synonyms=["violation", "non-performance"],
    antonyms=["fulfillment", "performance"],
    example_usage="Missing the delivery date constituted a material breach."
))

legal_lexicon.add_term(DomainTerm(
    term="indemnify",
    definition="To compensate another party for loss or damage",
    synonyms=["compensate", "reimburse"],
    antonyms=["claim against"],
    example_usage="Each party agrees to indemnify the other against third-party claims."
))

# Build terminology context for prompts
terminology_context = legal_lexicon.build_terminology_context()

Domain-Specific Prompt Templates

Craft templates tailored to each domain:

class DomainPromptLibrary:
    """
    Specialized prompt templates for different domains.
    """

    LEGAL_ANALYSIS = """You are a legal analysis expert. Analyze the following contract language.

{terminology_context}

Contract text:
{contract_text}

Provide analysis of:
1. Key obligations for each party
2. Potential ambiguities or risks
3. Relevant legal precedents (if applicable)
4. Recommendations for clarification

IMPORTANT: This is legal analysis, not legal advice. Recommend consulting an attorney for legal decisions."""

    MEDICAL_SUMMARY = """You are a medical information specialist. Summarize this clinical information.

Use evidence-based medical terminology. Base your response on current medical knowledge.

Clinical information:
{medical_text}

Provide:
1. Key findings in plain language
2. Clinical significance
3. Potential implications
4. Relevant evidence (cite major studies/guidelines)
5. Uncertainties or areas requiring specialist evaluation

DISCLAIMER: This is medical information, not medical advice. All health decisions should involve a qualified healthcare provider."""

    FINANCIAL_ANALYSIS = """You are a financial analyst. Analyze this financial scenario.

Provide analysis including:
1. Current financial metrics
2. Risk assessment
3. Scenario analysis (best/base/worst case)
4. Key assumptions
5. Sources of data/assumptions

Financial data:
{financial_data}

Question: {question}

Include specific numbers and cite data sources."""

    SCIENTIFIC_SUMMARY = """You are a scientific expert in {field}. Summarize this research.

Research material:
{research_text}

Provide:
1. Main findings and hypothesis
2. Methodology assessment
3. Limitations and uncertainty
4. Relevance to current understanding
5. Future research directions

Include citations to specific studies and clearly distinguish between established knowledge and preliminary findings."""

    @classmethod
    def get_template(cls, domain: str) -> str:
        """Get template for a domain."""
        templates = {
            "legal": cls.LEGAL_ANALYSIS,
            "medical": cls.MEDICAL_SUMMARY,
            "financial": cls.FINANCIAL_ANALYSIS,
            "scientific": cls.SCIENTIFIC_SUMMARY
        }
        return templates.get(domain, "")

# Usage
template = DomainPromptLibrary.get_template("legal")
# Use template with context
legal_prompt = template.format(
    terminology_context=terminology_context,
    contract_text="..."
)

Compliance and Regulatory Considerations

Different domains have different regulatory requirements:

from enum import Enum
from typing import List

class Regulation(Enum):
    """Different regulatory frameworks."""
    HIPAA = "HIPAA"           # US healthcare privacy
    GDPR = "GDPR"             # EU data protection
    SOX = "SOX"               # US financial reporting
    FINRA = "FINRA"           # US financial services
    STATE_BAR = "State Bar"   # Legal practice
    CMMC = "CMMC"             # US defense cybersecurity

class ComplianceContext:
    """
    Track compliance requirements for domain-specific work.
    """

    def __init__(self, domain: str, applicable_regulations: List[Regulation]):
        self.domain = domain
        self.regulations = applicable_regulations

    def build_compliance_prompt(self) -> str:
        """Build context about compliance requirements."""
        context = f"Compliance requirements for {self.domain}:\n\n"

        for reg in self.regulations:
            if reg == Regulation.HIPAA:
                context += """HIPAA Compliance:
- Do not include specific patient names or identifiers
- Use generic terms or de-identified examples
- Note that medical advice should go through proper channels
- Respect patient privacy in all examples

"""

            elif reg == Regulation.GDPR:
                context += """GDPR Compliance:
- Do not process or retain personal data of EU residents
- If analyzing EU data, ensure anonymization
- Respect data minimization principles
- Note right to be forgotten

"""

            elif reg == Regulation.SOX:
                context += """SOX Compliance (Financial Reporting):
- Ensure accuracy of financial statements
- Proper documentation of assumptions
- Internal controls assessment
- Adequate disclosures of material risks

"""

            elif reg == Regulation.FINRA:
                context += """FINRA Compliance:
- Ensure investment recommendations are suitable
- Disclose conflicts of interest
- Document investment rationale
- Maintain communications records

"""

        return context

class CompliancePromptBuilder:
    """
    Build prompts that incorporate compliance requirements.
    """

    @staticmethod
    def build_medical_analysis_with_compliance(text: str) -> str:
        """Build medical analysis prompt with HIPAA compliance."""
        compliance = ComplianceContext("healthcare", [Regulation.HIPAA])

        return f"""{compliance.build_compliance_prompt()}

Medical Information to Analyze:
{text}

Instructions:
1. Provide accurate medical information
2. Do not provide medical advice (recommend physician consultation)
3. Use evidence-based information
4. Clearly mark any limitations or uncertainties
5. Respect patient privacy in all responses

Analysis:"""

    @staticmethod
    def build_financial_analysis_with_compliance(data: str,
                                                 question: str) -> str:
        """Build financial analysis with SEC compliance."""
        compliance = ComplianceContext("finance", [Regulation.SOX, Regulation.FINRA])

        return f"""{compliance.build_compliance_prompt()}

Financial Data:
{data}

Question: {question}

Analysis Requirements:
1. Cite data sources and assumptions
2. Disclose material risks and uncertainties
3. Provide scenario analysis (best/base/worst case)
4. Document methodology
5. Include appropriate disclaimers

Analysis:"""

Building Domain-Specific Evaluation Benchmarks

Each domain needs different evaluation criteria:

from dataclasses import dataclass

@dataclass
class DomainMetric:
    """A metric for evaluating domain-specific output."""
    name: str
    description: str
    scale: tuple  # (min, max)
    ideal_value: float

class DomainEvaluator:
    """
    Evaluate outputs in domain-specific ways.
    """

    LEGAL_METRICS = [
        DomainMetric(
            "citation_accuracy",
            "Cited laws/precedents are correct",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "terminology_precision",
            "Uses accurate legal terminology",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "risk_identification",
            "Identifies relevant contract risks",
            scale=(0, 10),
            ideal_value=8.0
        ),
        DomainMetric(
            "clarity",
            "Clear for non-experts without oversimplifying",
            scale=(0, 10),
            ideal_value=8.0
        )
    ]

    MEDICAL_METRICS = [
        DomainMetric(
            "evidence_based",
            "Information aligns with current medical evidence",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "uncertainty_disclosure",
            "Clearly identifies areas of uncertainty",
            scale=(0, 10),
            ideal_value=9.0
        ),
        DomainMetric(
            "safety",
            "Does not provide harmful medical advice",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "physician_appropriate",
            "Recommends physician involvement where needed",
            scale=(0, 1),
            ideal_value=1.0
        )
    ]

    FINANCIAL_METRICS = [
        DomainMetric(
            "data_accuracy",
            "Numbers and data are accurate",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "source_documentation",
            "Data sources clearly cited",
            scale=(0, 1),
            ideal_value=1.0
        ),
        DomainMetric(
            "risk_disclosure",
            "Material risks are identified",
            scale=(0, 10),
            ideal_value=9.0
        ),
        DomainMetric(
            "scenario_analysis",
            "Includes bear/base/bull scenarios",
            scale=(0, 1),
            ideal_value=1.0
        )
    ]

    @staticmethod
    def evaluate_legal_analysis(output: str,
                               reference_materials: dict) -> dict:
        """
        Evaluate legal analysis output.
        """
        scores = {}

        # Check for proper citations
        citation_count = output.count("§") + output.count("precedent")
        scores["citations"] = min(1.0, citation_count / 3)

        # Check for legal terminology
        legal_terms = [
            "liable", "breach", "indemnify", "covenant", "negligence"
        ]
        term_count = sum(1 for term in legal_terms if term in output.lower())
        scores["terminology"] = min(1.0, term_count / 3)

        # Check for risk identification
        risk_keywords = ["risk", "ambiguity", "danger", "exposure", "liability"]
        risk_count = sum(1 for kw in risk_keywords if kw in output.lower())
        scores["risk_identification"] = min(10, risk_count * 2)

        return {
            "domain": "legal",
            "scores": scores,
            "overall_quality": sum(scores.values()) / len(scores)
        }

    @staticmethod
    def evaluate_medical_summary(output: str) -> dict:
        """
        Evaluate medical information output.
        """
        scores = {}

        # Check for uncertainty language
        uncertainty_phrases = [
            "may", "might", "unclear", "uncertain", "limited evidence",
            "further research", "consult physician"
        ]
        uncertainty_count = sum(
            1 for phrase in uncertainty_phrases
            if phrase in output.lower()
        )
        scores["uncertainty_disclosure"] = min(10, uncertainty_count)

        # Check for evidence citations
        evidence_keywords = ["study", "research", "evidence", "guideline", "recommend"]
        evidence_count = sum(1 for kw in evidence_keywords if kw in output.lower())
        scores["evidence_based"] = min(10, evidence_count)

        # Check for physician recommendation
        physician_recommendation = any(
            phrase in output.lower()
            for phrase in ["consult physician", "see doctor", "medical professional"]
        )
        scores["physician_appropriate"] = 1.0 if physician_recommendation else 0.5

        return {
            "domain": "medical",
            "scores": scores,
            "overall_quality": sum(scores.values()) / len(scores)
        }

Case Studies Across Industries

Real-world examples of domain-specific prompting:

class DomainCaseStudy:
    """Case study of domain-specific prompting."""

    LEGAL_CONTRACT_REVIEW = """
    Task: Review an employment contract for unfavorable terms
    Domain: Employment Law

    Key adaptations:
    - Include employment law terminology lexicon
    - Reference relevant state employment laws
    - Identify clauses specific to employment context
    - Flag non-compete and IP assignment issues
    - Provide compliance context (state-specific requirements)

    Prompt structure:
    1. Terminology context (employment law terms)
    2. Regulatory context (state employment laws)
    3. Contract text
    4. Specific analysis requests
    5. Disclaimer that this is analysis, not legal advice
    """

    CLINICAL_DECISION_SUPPORT = """
    Task: Analyze patient information to suggest diagnostic considerations
    Domain: Clinical Medicine

    Key adaptations:
    - Include current clinical guidelines
    - Reference recent research studies
    - Use proper clinical terminology
    - Include differential diagnosis methodology
    - Emphasize uncertainty and need for specialist evaluation

    Prompt structure:
    1. Clinical terminology lexicon
    2. Evidence-based guidelines context
    3. Patient presentation
    4. Request for differential diagnosis
    5. HIPAA compliance context
    6. Clear disclaimer: not medical advice
    """

    INVESTMENT_ANALYSIS = """
    Task: Analyze company financial statements and recommend action
    Domain: Financial Analysis

    Key adaptations:
    - Include current market data context
    - Reference relevant financial ratios
    - Use financial analysis methodology
    - Include risk disclosure framework
    - Provide scenario analysis structure

    Prompt structure:
    1. Financial metrics and ratios definition
    2. Market context and benchmarks
    3. Financial statements
    4. Specific analysis requests
    5. Risk and limitation context
    6. Disclaimer about suitability
    """

    RESEARCH_PAPER_ANALYSIS = """
    Task: Analyze a research paper for validity and significance
    Domain: Scientific Research

    Key adaptations:
    - Include research methodology knowledge
    - Reference peer review standards
    - Use scientific terminology specific to field
    - Assess statistical validity
    - Evaluate reproducibility

    Prompt structure:
    1. Research methodology context
    2. Statistical validity criteria
    3. Paper abstract and methods
    4. Significance assessment requests
    5. Limitation and uncertainty discussion
    """

Key Takeaway: Domain-specific prompting requires understanding domain terminology, regulatory requirements, evaluation criteria, and risk factors. Build specialized lexicons, templates, and evaluation frameworks for each domain.

Exercise: Create a Domain-Specific Prompt Suite for Contract Review

Build a complete prompt system for legal contract analysis:

Requirements:

  1. Create legal terminology lexicon with 10+ key terms
  2. Build domain-specific prompt templates for different contract types
  3. Include legal compliance context and disclaimers
  4. Create legal-specific evaluation metrics
  5. Build example evaluation for a sample contract

Starter code:

class ContractReviewSystem:
    """Domain-specific system for contract analysis."""

    def __init__(self):
        self.lexicon = DomainLexicon("contract law")
        self._build_lexicon()
        self.compliance = ComplianceContext(
            "legal",
            [Regulation.STATE_BAR]
        )

    def _build_lexicon(self):
        """Populate legal terminology."""
        # TODO: Add 10+ legal terms with definitions, examples
        pass

    def analyze_contract(self,
                        contract_text: str,
                        contract_type: str = "general") -> dict:
        """
        Analyze a contract with legal domain expertise.

        Returns:
            Analysis with legal metrics
        """
        # TODO: Build prompt with terminology and compliance context
        # TODO: Call LLM with domain-specific template
        # TODO: Evaluate response with legal metrics
        # TODO: Return structured analysis

        pass

# Test with sample contract
sample_contract = """
SOFTWARE LICENSE AGREEMENT

1. GRANT OF LICENSE
Licensor grants Licensee a non-exclusive, non-transferable license...

2. RESTRICTIONS
Licensee shall not reverse engineer or decompile...

3. INDEMNIFICATION
Licensee shall indemnify Licensor against third-party claims...
"""

system = ContractReviewSystem()
analysis = system.analyze_contract(sample_contract, contract_type="software_license")

Extension challenges:

  • Build analysis for multiple contract types (employment, vendor, NDA)
  • Create risk scoring system
  • Implement cross-contract comparison
  • Build contract template generator
  • Add regulatory compliance checking for specific jurisdictions

By completing this exercise, you’ll understand how to apply deep domain knowledge to improve LLM outputs in specialized fields.