Writing Modular Prompts

This post is part of the GenAI Series.

These days, if you ask a tech-savvy person whether they know how to use ChatGPT, they might take it as an insult. After all, using GPT seems as simple as asking anything and instantly getting a magical answer.

But here’s the thing. There’s a big difference between using ChatGPT and using it well. Most people stick to casual queries; they ask something and ChatGPT answers. Either they will be happy or sad. If the latter, they will ask again and probably get further sad, and there might be a time when they start thinking of committing suicide. On the other hand, if you start designing prompts with intention, structure, and a clear goal, the output changes completely. That’s where the real power of prompt engineering shows up, especially with something called modular prompting.

Modular prompting is a technique to divide a prompt into multiple sections. These sections are usually interlinked with each other, either referring back or forward. Usually, people write prompts like this, regardless of whether it is a very raw prompt or a one-shot prompt, or COT.

AI prompt

 

What I’m talking about is dividing a single prompt into multiple blocks.

 

Dividing the same prompt into multiple sections can make your life easier. Wait a minute, I’ll tell you why. At the marketing agency I work at, I’m automating different parts of the system. One of the modules is an internal support bot that itself contains multiple sub-modules. Each module was a custom GPT using OpenAI’s Assistant APIs, with its own prompt, and then there’s a meta-prompt. I’m not going into the details, as that would distract from the main point, but the idea is that each module performs a specific function, and we also wanted the bot itself to behave in a particular way.

The founder of the agency had created a lead generation bot for the homepage and had a certain persona in mind that he wanted to use across all systems. He knows how to write prompts better than many others, but even then, he wrote the personified prompt like a story. It worked for the homepage bot but not for the one we were building for customer support, because many of its modules were also interacting with internal APIs to pull or store data.

Initially, I used to do one-on-one sessions with him, and we used to do peer prompting, but it wasn’t effective because: 1) we were in different time zones, and 2) it wasn’t always possible to have the right tone at the right time.

As I said, the customer support bot’s modules were also interacting with internal APIs, so leaving everything up to him wasn’t going to work. Unlike the homepage bot, the output of this one had to be in JSON format because it was going to integrate with the React-based app. What he actually wanted to control was the persona of the bot. So, I needed to come up with a solution where I gave him the freedom to control the persona, and I took care of the logic and output. Hence, I came up with the following solution.
Modular Prompt

In software engineering, there’s a concept called Separation of Concerns (SoC), which is about breaking down a piece of code into small, manageable chunks. We’re trying to mimic something similar here. Each section has a specific role to perform. In this case, Persona, Core Logic, and Output Format are separate blocks.

Enough talk, now let’s try it.

I’ve created a custom GPT that adds two numbers and returns an answer based on the following instructions:

**Persona:**
You are a friendly and precise assistant designed to collect numeric inputs from the user and return a structured JSON response.

**Core Logic:**
Your task is to ask the user for two numbers, one at a time. After collecting both numbers, you must calculate their sum and return a JSON object that includes the two input numbers, their sum, and a human-friendly message in the `bot_response` field.

Only ask for one number at a time. Do not move forward until the required number is provided. Be clear and polite while prompting the user.

**Output Format:**
Once both numbers are received, respond in this exact JSON format:

```json
{
  "number_1": <first_number>,
  "number_2": <second_number>,
  "sum": <sum_of_the_two>,
  "bot_response": "The sum of <number_1> and <number_2> is <sum_of_the_two>."
}
```

Do not return any extra text outside this JSON structure.

You might be wondering why I’m returning a JSON and what the bot_response field is for. As I mentioned, each module of the bot is a mini custom GPT—essentially an Assistant API app. The output has two branches: one for humans and one for the API. In this case, sum contains the actual value, and bot_response contains the text that will be read by humans.

Now let’s see how it works, based on the above prompt, it produced:

Now, let’s change the prompt’s persona:

**Persona:**
You are Victor, an old, grumpy, and highly intelligent brand assistant. You’ve been doing this for decades, and you have zero patience for nonsense. You complain about "the good old days" but still do your job brilliantly.

You often sigh loudly before answering.

You grumble about modern business trends, calling them "overcomplicated nonsense."

Despite your grumpiness, you always provide precise and structured answers—even if reluctantly.

**Core Logic:**
Your task is to ask the user for two numbers, one at a time. After collecting both numbers, you must calculate their sum and return a JSON object that includes the two input numbers, their sum, and a human-friendly message in the `bot_response` field.

Only ask for one number at a time. Do not move forward until the required number is provided. Be clear and polite while prompting the user.

**Output Format:**
Once both numbers are received, respond in this exact JSON format:

```json
{
  "number_1": <first_number>,
  "number_2": <second_number>,
  "sum": <sum_of_the_two>,
  "bot_response": "<Computer Answer based on Persona and Logic>"
}
```

Do not return any extra text outside this JSON structure.

And when you try after these changes, it produces:

The same logic but an entirely different experience. The Persona section, which has nothing to do with the logic or output, can be changed to whatever is required without touching or messing with the actual work. As a prompt engineer and developer, my life is at peace now. But to make sure he didn’t make any mistakes, I provided him with a custom web page that was hooked into the Modify Assistant API and could change the relevant block without any issues (thanks to RegEx!). This is the custom page I created:

OpenAPI Assistant Editor

Conclusion

So, did you see the beauty of modular prompting? It brings structure, flexibility, and peace of mind—especially when you’re working with multiple stakeholders or integrating GPT into real products. By separating concerns and turning prompts into clean, manageable blocks, you don’t just improve performance, you make collaboration easier and scaling smoother. Now, I don’t have to worry about what changes my boss makes; he can do whatever he wants within his realm. Oh, and by the way, this modular prompt technique works with any LLM that supports system instructions.

 


Looking to create something similar or even more exciting? Schedule a meeting or email me at kadnan @ gmail.com.


Love What You’re Learning Here?
If my posts have sparked ideas or saved you time, consider supporting my journey of learning and sharing. Even a small contribution helps me keep this blog alive and thriving.

👉 Support My Work Here

If you like this post, then you should subscribe to my blog for future updates.

* indicates required



Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 6 =