# How to Customize Prompt Templates Using the md_template.toml File in MathModelAgent

> Easily customize prompt templates in MathModelAgent by editing the md_template.toml file. Learn how to modify writer-agent prompts for personalized interactions.

- Repository: [Sanjin/mathmodelagent](https://github.com/jihe520/mathmodelagent)
- Tags: how-to-guide
- Published: 2026-03-04

---

**You customize prompt templates in MathModelAgent by editing the TOML-formatted multiline strings in [`backend/app/config/md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/config/md_template.toml), which the `get_config_template()` function automatically loads and injects into the writer-agent prompts at runtime.**

The `jihe520/mathmodelagent` repository generates automated mathematical modeling reports by combining structured data with predefined writing instructions. When you need to adjust the wording, structure, or formatting of generated sections like the title page or problem restatement, you customize prompt templates using the [`md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/md_template.toml) file. This configuration-driven approach decouples prompt engineering from application logic, allowing you to refine AI outputs without modifying Python source code.

## How the Template System Works

The backend stores all writing instructions in a single **TOML configuration file** located at [`backend/app/config/md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/config/md_template.toml). Each key in this file corresponds to a specific section of the final report—such as `firstPage` for the title and abstract, or `RepeatQues` for the problem restatement—and contains a triple-quoted multiline string that defines the exact wording the writer-agent should use.

According to the source code, the system supports multiple competition templates via the `CompTemplate` enum, with `CompTemplate.CHINA` serving as the default that loads the Chinese-language template set.

## Loading the Configuration

The utility function `get_config_template()` in [`backend/app/utils/common_utils.py`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/utils/common_utils.py) handles file I/O and parsing:

```python

# backend/app/utils/common_utils.py

def get_config_template(comp_template: CompTemplate = CompTemplate.CHINA) -> dict:
    if comp_template == CompTemplate.CHINA:
        return load_toml(os.path.join("app", "config", "md_template.toml"))

```

This helper reads the TOML file (lines 46–48) and returns a Python dictionary where keys match the section names used throughout the workflow. Because the function is called during task execution rather than at startup, any edits you save to [`md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/md_template.toml) take effect immediately on the next report generation without requiring a server restart.

## Injecting Templates into Writing Flows

When the writing stage initiates, the `Flows.get_write_flows` method in [`backend/app/core/flows.py`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/core/flows.py) constructs the final prompt for each section by **concatenating** the problem background, model-building results, and the corresponding template string from the loaded configuration:

```python

# backend/app/core/flows.py (excerpt)

flows = {
    "firstPage": f"""...{config_template["firstPage"]}...""",
    "RepeatQues": f"""...{config_template["RepeatQues"]}...""",
    # … additional sections

}

```

The `config_template` dictionary passed into this method originates from `get_config_template()`, ensuring that any modifications you make in the TOML file propagate directly into the generated prompts.

## Customizing Your Templates

### Editing the TOML Structure

Open [`backend/app/config/md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/config/md_template.toml) in a text editor. The file uses standard TOML syntax where each value is a **triple-quoted string** (`""" … """`), preserving line breaks and Markdown formatting:

```toml
firstPage = """

# 标题： 基于全文取一个标题

例子：基于哈里斯鹰算法的农业种植优化模型研究
摘要
...
"""
RepeatQues = """

# 一、问题重述

## 1.1 问题背景

...
"""

```

You may freely edit the text, adjust heading levels, insert Markdown tables, or embed LaTeX formulas for mathematical notation.

### Managing Placeholders Safely

The templates rely on curly-brace placeholders such as `{问题}` and `{背景}` that the workflow substitutes with actual content at runtime. **Keep these placeholder names unchanged** unless you also modify the substitution logic in [`flows.py`](https://github.com/jihe520/mathmodelagent/blob/main/flows.py) or upstream code. If you add a custom placeholder like `{自定义字段}`, you must provide the corresponding value when building the writer prompt, otherwise the final report will contain the raw placeholder text.

### Runtime Template Overrides

For temporary changes without editing the file, load the template dictionary and modify it in memory before passing it to the Flow:

```python
from app.utils.common_utils import get_config_template

# Load the default Chinese template set

tpl = get_config_template()

# Override the firstPage template for this execution only

tpl["firstPage"] = """# 新标题模板

请基于以下背景撰写标题、摘要和关键词：
{背景}
"""

# Pass the modified dictionary to the Flow

flows = Flows(questions).get_write_flows(user_output, tpl, bg_ques_all)

```

## Verifying Your Changes

To confirm that your edits are being read correctly, run an isolated loading script:

```python
from app.utils.common_utils import get_config_template

templates = get_config_template()

print("First-page template snippet:")
print(templates["firstPage"][:200])  # Display first 200 characters

```

This outputs the current content of the `firstPage` key, allowing you to validate formatting before triggering a full report generation.

## Summary

- **Edit** [`backend/app/config/md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/config/md_template.toml) to modify the wording and structure of report sections.
- **Use** triple-quoted TOML strings to preserve multiline formatting, Markdown syntax, and LaTeX.
- **Maintain** placeholder names like `{问题}` and `{背景}` to ensure proper content substitution.
- **Load** templates via `get_config_template()` in [`backend/app/utils/common_utils.py`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/utils/common_utils.py), which parses the file into a dictionary.
- **Inject** templates automatically into the writing workflow through `Flows.get_write_flows` in [`backend/app/core/flows.py`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/core/flows.py).
- **Apply** changes immediately without restarting the server, as the TOML file is read on each task execution.

## Frequently Asked Questions

### Can I add entirely new sections to the template file?

Yes, you can add new keys to [`md_template.toml`](https://github.com/jihe520/mathmodelagent/blob/main/md_template.toml) following the triple-quoted string format. However, you must also update [`backend/app/core/flows.py`](https://github.com/jihe520/mathmodelagent/blob/main/backend/app/core/flows.py) to reference the new key in the `flows` dictionary within `get_write_flows`, otherwise the new template will not be injected into any prompt.

### Do I need to restart the server after editing md_template.toml?

No. The `get_config_template()` function loads the TOML file during each task execution, not at application startup. Save your changes to disk, and the next report generation will automatically use the updated templates.

### What placeholder syntax should I use when customizing templates?

The system expects curly-brace placeholders such as `{问题}`, `{背景}`, and `{自定义字段}`. These strings are replaced with actual content by the workflow logic. If you introduce new placeholders, ensure the code that builds the prompts includes substitution logic for your new variables.

### Is it possible to use Markdown tables and LaTeX inside the templates?

Yes. Because TOML triple-quoted strings preserve literal text including newlines, you can embed complex Markdown structures like tables, code fences, and LaTeX mathematical expressions (e.g., `$$y = ax^2 + bx + c$$`) directly inside the template values.