Avoid Inlining Variables in Your Prompts

TLDR: Don't embed variables directly within AI prompt instructions as it can lead to confusing, "mangled" text. Instead, clearly label and separate the variables to ensure the AI receives unambiguous directions, enhancing its ability to generate accurate and relevant content.


Avoid Inlining Variables in Prompts

You might have a natural tendency to do something we call "inlining variables," but you should avoid it.

Inlining variables is when you use variables in your prompt in a natural way "inline" with the instructions of the prompt.

Here's an example:

Write a blog post about the {{Topic}} so that the {{Audience}} is hooked and find the post helpful and interesting. Write in a clear {{Style}} and {{Tone}} so we can deliver on the {{Purpose}} of the blog post for the audience.

Reading this example sounds smooth, right? Go ahead, read it aloud to yourself. It flows well enough because the name of the variables "fit" into the passage fine.

But there's a problem brewing. Can you guess what that might be?

What's wrong with this?

Imagine for a moment these values are saved in your variables:

  • Topic = Ramen in Tokyo

  • Audience = Young couples on their first trip to Tokyo with small children in tow. Looking for interesting places to see and budget food that is still delicious.

  • Style = upbeat and informative

  • Tone = friendly and understanding

  • Purpose = to find budget friendly ramen shops that are kid-friendly while also catching that authentic Japan vibe

Now let's replace those variables with their contents so we can see what the AI will receive:

Write a blog post about the Ramen in Tokyo so that the Young couples on their first trip to Tokyo with small children in tow is hooked and find the post helpful and interesting. Write in a clear upbeat and informative and friendly and understanding so we can deliver on the to find budget friendly ramen shops that are kid-friendly while also catching that authentic Japan vibe of the blog post for the audience.

Now read this out loud to yourself. It's janky isn't it?

Inlining variables leads to "mangled prompts" like this example shows -- unless you know what you're doing and do it on purpose.

It's not hard to imagine even AI will have difficulty with a mangled prompt like this. That's what you want to avoid. You want the AI to have the best possible instructions to generate the results you're truly after.

What to do instead

Rather than inline, extract out what you want to pass the AI and label it.

For example:

Topic: Ramen in Tokyo

Audience: Young couples on their first trip to Tokyo with small children in tow. Looking for interesting places to see and budget food that is still delicious.

Style: Upbeat and informative

Tone: Friendly and understanding

Purpose: To find budget friendly ramen shops that are kid-friendly while also catching that authentic Japan vibe

Write a blog post about the topic above. Keep in mind the target audience and write in the specified style and tone to achieve the stated purpose of the post.

By extracting out the variables and labeling them, you provide the AI with clear context on how to use each piece of information. The resulting prompt is much easier for the AI to parse and understand compared to the inlined version.

Some food for thought. Consider putting things like "how" the AI should write into a role rather than in the prompt (such as tone). That way you can reuse the role for any of your actions that need to write in this way.

Was this helpful?