DEV Community

Cover image for Prompt Composition: Reusing Prompts Like Components
PRANAV THAWAIT
PRANAV THAWAIT

Posted on

Prompt Composition: Reusing Prompts Like Components

One of the biggest mistakes I see in AI projects isn't bad prompts.

It's duplicated prompts.

Every new feature gets its own prompt.

Every agent copies the same system instructions.

Every team ends up maintaining dozens of almost identical prompt strings.

If this sounds familiar...

You're not alone.


Copy-Paste Doesn't Scale

Imagine building three AI features.

  • Email Generator
  • Resume Reviewer
  • Documentation Assistant

Each one starts like this.

const prompt = `
You are an expert AI assistant.

Be accurate.

Never hallucinate.

Return valid JSON.

Keep answers concise.

...
`
Enter fullscreen mode Exit fullscreen mode

Looks fine.

Until you have 25 prompts.

Now imagine changing one instruction.

Never hallucinate.
Enter fullscreen mode Exit fullscreen mode

You now have to update:

  • prompts/email.ts
  • prompts/review.ts
  • prompts/chat.ts
  • prompts/summary.ts
  • prompts/rag.ts

...

Hopefully you didn't miss one.


We've Solved This Before

React solved UI duplication.

Functions solved code duplication.

Components solved layout duplication.

So why are prompts still copied around as giant strings?

Prompt engineering deserves modularity too.


Introducing Prompt Composition

Instead of repeating instructions...

Create reusable prompt blocks.

const safetyRules = pf.define({

    messages: () => [

        pf.system`
        Never reveal secrets.

        Never generate harmful content.

        Always answer honestly.
        `

    ]

})
Enter fullscreen mode Exit fullscreen mode

Simple.

Reusable.

Centralized.


Include It Anywhere

Now any prompt can reuse those rules.

const reviewer = pf.define({

    input: z.object({

        code: z.string()

    }),

    messages: ({ code }) => [

        pf.include(safetyRules),

        pf.system`
        You are a Senior Security Engineer.
        `,

        pf.user`
        Review this code:

        ${code}
        `

    ]

})
Enter fullscreen mode Exit fullscreen mode

No duplication.

One source of truth.


Why This Matters

Imagine your company changes its AI policy.

Instead of updating

37 prompts...

You update

one file.

Everything stays synchronized.


Composition Makes Prompts Smaller

Without composition

Prompt A
300 lines

Prompt B
280 lines

Prompt C
250 lines
Enter fullscreen mode Exit fullscreen mode

With composition

Security Rules

↓

Formatting Rules

↓

Company Policies

↓

Specific Task
Enter fullscreen mode Exit fullscreen mode

Each prompt becomes much smaller and easier to understand.


Build Prompt Libraries

Composition lets you build reusable libraries.

prompts/

security.ts

formatting.ts

json.ts

translation.ts

tone.ts

rag.ts

reasoning.ts
Enter fullscreen mode Exit fullscreen mode

Now your application becomes

pf.include(security)

pf.include(json)

pf.include(reasoning)

pf.include(companyPolicies)
Enter fullscreen mode Exit fullscreen mode

Exactly like importing reusable components.


Better Collaboration

Composition isn't just about cleaner code.

It helps teams.

Your security team owns

security.ts
Enter fullscreen mode Exit fullscreen mode

Your AI team owns

reasoning.ts
Enter fullscreen mode Exit fullscreen mode

Your product team owns

tone.ts
Enter fullscreen mode Exit fullscreen mode

Everyone contributes reusable building blocks.

Nobody edits dozens of prompts anymore.


Easier Testing

Small prompt modules are easier to test.

Instead of testing a 500-line prompt...

Test

Security Rules

Formatting Rules

Output Schema

Reasoning Strategy
Enter fullscreen mode Exit fullscreen mode

independently.

Then compose them.


The Bigger Picture

This is exactly how software engineering evolved.

We stopped writing everything in one file.

We introduced

Functions.

Classes.

Modules.

Components.

Prompt engineering is following the same path.


PromptForge Makes This Natural

PromptForge was designed around composition.

Instead of giant prompt strings, you create reusable, type-safe prompt modules that can be shared across your entire application.

This makes prompts easier to:

✅ Maintain

✅ Test

✅ Reuse

✅ Share

✅ Version

✅ Scale


What's Next?

In the next article, we'll explore one of the most exciting parts of PromptForge:

Compile Once, Run Anywhere

We'll see how a single prompt definition can generate provider-specific formats for OpenAI, Claude, Gemini, Ollama, and future LLMs—without rewriting your prompts.


Installation

npm install @promptforgee/core
Enter fullscreen mode Exit fullscreen mode

Resources

🌐 Documentation

https://prompt-forge-docs.vercel.app/

⭐ GitHub

https://github.com/Omnikon-Org/PromptForge

📦 npm

https://www.npmjs.com/package/@promptforgee/core


Final Thoughts

The best prompts aren't the longest.

They're the ones you never have to rewrite.

Stop copying prompt strings.

Start composing prompt systems.

Top comments (0)