# Optimizing Prompts for Financial Services Use Cases with Claude

> Learn to optimize Claude prompts for financial services. Ensure regulatory adherence precision and deterministic output with XML tagging and system role definition.

- Repository: [Anthropic/prompt-eng-interactive-tutorial](https://github.com/anthropics/prompt-eng-interactive-tutorial)
- Tags: tutorial
- Published: 2026-03-09

---

**Financial services prompts require strict regulatory adherence, precise citations, and deterministic output formats that separate raw legal text from instructions using XML tagging and explicit system role definition.**

Financial institutions deploying Claude for compliance checks, tax advice, or investment policy queries must engineer prompts that prioritize accuracy and traceability. The `anthropics/prompt-eng-interactive-tutorial` repository provides a battle-tested architecture for these high-stakes use cases, specifically demonstrated in Exercise 9.1. This guide extracts the production patterns from that codebase to help you build audit-ready, regulation-aware prompt workflows.

## Core Architecture for Financial Services Prompts

The tutorial establishes four non-negotiable principles for **optimizing prompts for financial services use cases with Claude**:

- **Variable Injection**: Isolate regulatory text (e.g., tax codes, SEC filings) in input variables rather than hard-coding them, enabling runtime swapping of legal corpuses without prompt restructuring.
- **Expert Role Definition**: Use the system prompt to assign a specific professional persona (e.g., "master tax accountant") that anchors the model's tone and domain expertise.
- **Deterministic Output Schema**: Mandate XML-structured responses (e.g., `<quotes>` followed by `<answer>`) to ensure consistent parsing and audit trails.
- **Ordered Element Concatenation**: Assemble the final prompt through explicit string operations that guarantee section ordering, preventing prompt injection or misalignment.

### Separate Data from Instructions

In `Anthropic 1P/09_Complex_Prompts_from_Scratch.ipynb`, the solution stores dynamic regulatory content in a `{TAX_CODE}` placeholder. This separation allows the same prompt template to service different compliance domains—swapping IRS code for AML policies or SEC regulations—without editing the core instructions.

### Enforce Structured Output with XML Tags

The reference implementation in `Anthropic 1P/hints.py` (specifically the `exercise_9_1_solution` at line 1007) requires Claude to wrap source citations in `<quotes>` tags and final conclusions in `<answer>` tags. This structured approach enables automated validation pipelines to verify that every recommendation is grounded in provided legal text.

## Building a Tax Compliance Prompt from Scratch

Below is a runnable implementation based on the repository's [`hints.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/hints.py) solution and [`utils.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/utils.py) helper architecture. This example demonstrates how to construct a prompt that answers tax questions using specific code sections while maintaining citation accuracy.

```python
from utils import get_completion

# ----------------------------------------------------------------------

# 1️⃣  INPUT VARIABLES (runtime data injection)

# ----------------------------------------------------------------------

QUESTION = "How long do I have to make an 83b election?"
TAX_CODE = """
(a) General rule …
(b) Election to include in gross income …
...
"""

# ----------------------------------------------------------------------

# 2️⃣  SYSTEM PROMPT (role definition + schema)

# ----------------------------------------------------------------------

SYSTEM_PROMPT = """You are a master tax accountant. Your task is to answer user questions using any provided reference documentation.

Here is the material you should use to answer the user's question:
<docs>
{TAX_CODE}
</docs>

When you answer, follow this exact structure:
<example>
<question>
{QUESTION}
</question>

<answer>
<quotes>…relevant excerpts from <docs>...</quotes>

<answer>…your concise answer here...</answer>
</example>
"""

# ----------------------------------------------------------------------

# 3️⃣  COMBINE ELEMENTS (deterministic assembly)

# ----------------------------------------------------------------------

prompt = (
    SYSTEM_PROMPT
    .replace("{TAX_CODE}", TAX_CODE)
    .replace("{QUESTION}", QUESTION)
)

# ----------------------------------------------------------------------

# 4️⃣  EXECUTE

# ----------------------------------------------------------------------

response = get_completion(prompt)
print(response)

```

**Key Implementation Details:**

- **`get_completion`**: Imported from [`utils.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/utils.py), this helper standardizes API parameters (temperature, max tokens) across the tutorial suite, ensuring consistent Claude behavior for financial queries.
- **Placeholder Replacement**: The `.replace()` chain guarantees that the regulatory text is inserted at the exact location specified by the `<docs>` tags, preventing context misalignment.
- **Reusable Template**: The `SYSTEM_PROMPT` string can be stored as a constant and reused across different tax years or regulatory updates by simply changing the `TAX_CODE` variable.

## Repository Structure and Key Files

The `anthropics/prompt-eng-interactive-tutorial` repository organizes these patterns into modular components:

| File | Purpose | Critical Component |
|------|---------|-------------------|
| `Anthropic 1P/09_Complex_Prompts_from_Scratch.ipynb` | Interactive walkthrough of Exercise 9.1, demonstrating the financial services chatbot workflow. | Problem statement and step-by-step assembly logic. |
| `Anthropic 1P/hints.py` | Production-ready solution code. | `exercise_9_1_solution` function at line 1007 containing the complete prompt template. |
| [`utils.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/utils.py) | API abstraction layer. | `get_completion` wrapper that handles Claude API authentication and parameter defaults. |
| `AmazonBedrock/boto3/09_Complex_Prompts_from_Scratch.ipynb` | AWS-native adaptation. | Same financial services logic implemented via Boto3 for Bedrock runtime. |

## Summary

- **Optimizing prompts for financial services use cases with Claude** requires strict separation of legal source material from instructional text using variable injection.
- The `anthropics/prompt-eng-interactive-tutorial` repository demonstrates this via `exercise_9_1_solution` in [`hints.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/hints.py), which employs XML tagging (`<docs>`, `<quotes>`, `<answer>`) to enforce citation traceability.
- Use deterministic string concatenation (as shown in `09_Complex_Prompts_from_Scratch.ipynb`) rather than f-strings for complex prompts to maintain exact section ordering.
- Implement the `get_completion` pattern from [`utils.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/utils.py) to standardize API configuration across compliance applications.
- Adapt the architecture for different financial domains (SEC, AML, tax) by swapping the content of input variables while preserving the prompt structure.

## Frequently Asked Questions

### How do I prevent hallucinations when Claude answers financial regulatory questions?

Require explicit source attribution using XML tags. According to the repository's implementation in [`hints.py`](https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/main/hints.py), wrapping citations in `<quotes>` tags and mandating that answers derive only from the provided `<docs>` content forces the model to ground responses in the supplied regulatory text rather than training data.

### Can I use this prompt architecture with Amazon Bedrock instead of direct API access?

Yes. The repository includes `AmazonBedrock/boto3/09_Complex_Prompts_from_Scratch.ipynb`, which implements the same Exercise 9.1 financial services patterns using the Boto3 client. The prompt template structure remains identical; only the `get_completion` implementation changes to use Bedrock's `invoke_model` API.

### Why use `.replace()` instead of f-strings for combining prompt elements?

Using `.replace()` on a template string (as demonstrated in the `exercise_9_1_solution` approach) ensures deterministic ordering of prompt sections and prevents accidental evaluation of curly braces within the regulatory text itself. This method is safer when injecting large legal documents that may contain special characters or braces.

### How do I adapt this for real-time compliance checking against multiple regulations?

Store each regulation (e.g., SEC Rule 144, FINRA 2210) in separate variables or a dictionary, then dynamically populate the `{TAX_CODE}` placeholder with the relevant subset based on the query context. The prompt architecture in `Anthropic 1P/09_Complex_Prompts_from_Scratch.ipynb` supports this by treating the regulatory content as modular input data separate from the fixed system instructions.