Pular para o conteúdo
← Back to Skalablog

Published article

How to do prompt engineering that works

Software EngineeringAnthropicOpenAIGemini

Prompt engineering works best when you understand what the model actually does: read tokens, weigh probabilities, and complete text. Structure helps, but the real gains come from a repeatable method, controlled generation parameters, and letting the LLM draft prompts for you.

Does prompt structure matter: XML, JSON, Markdown or YAML?

Structure matters far less than people assume, and no single format wins across all models. The honest answer is that XML, JSON, Markdown and YAML all work, because what a modern LLM needs is clear boundaries between roles, instructions, context and examples, not a specific tag syntax.

XML tags such as ... are popular because they make it easy to see where a section starts and ends, which helps on long prompts. Anthropic documents this tag-based style in its own prompt engineering guide. Markdown with H1 and H2 headings does the same job in a lighter way, and JSON is convenient when the answer must be parsed by code.

When a new 'definitive' format trends for a few days, treat it as a repackaging of the same idea: separate information clearly so the model can tell what each piece is for. If you swap from one LLM to another and quality drops, check the model's own documentation first, then adjust the structure, rather than rewriting the whole prompt from scratch.

What is the PRICE method for writing complete prompts?

PRICE is a simple checklist that stops you from sending vague prompts. It stands for Persona, Reasoning, Instructions, Constraints and Examples, and it covers the five things a complete prompt usually needs to produce reliable answers.

Here is what each part does:

  • P — Persona: tell the model who it is ('act as a senior React reviewer').- R — Reasoning: ask for step-by-step thinking, which opens the door to techniques like Chain of Thought or Tree of Thought.- I — Instructions: state the objective and the task in plain terms.- C — Constraints: say what must never happen, what to avoid, what to refuse.- E — Examples or output format: demand Markdown, JSON or a specific shape when you will process the answer programmatically.

The persona line also matters for cost and tone. A prompt asking for a 'creative, humorous, counterintuitive' answer will produce very different output from the same question asked plainly, which is exactly what generation parameters, covered below, amplify or suppress.

Should you let the LLM write your prompts for you?

Yes, and this is the most underrated move in prompt engineering. Instead of hunting for the prompt some influencer is using, ask the model itself: 'create a prompt for solving X, to be used by model Y, following that model's best practices'. The model drafts a longer, better-structured prompt than most people write by hand, often choosing Markdown or tags on its own.

This 'prompt to prompt' loop does not make fundamentals useless. You still need to understand roles, reasoning, constraints and output formats, otherwise you cannot judge whether the generated prompt is good or improve it. The foundation is what lets you recognize that a trending technique is just another way to do something you already know.

What about DSPy and prompt-free approaches?

DSPy, an open-source framework from the Stanford NLP group that appeared around 2023, goes one step further: instead of hand-written prompt strings, you declare your program's input and output in Python and DSPy compiles the prompts and can optimize them against a metric. It also supports reasoning strategies such as ReAct, the 2022 'reasoning plus acting' agent pattern, which has nothing to do with the JavaScript library of a similar name. For JavaScript, community ports exist, though the reference implementation is Python-first.

The practical takeaway: write the first prompt yourself with PRICE, use the LLM to expand and refine it, and move to a framework like DSPy when prompts become an engineering problem inside your product rather than a one-off chat message.

What are tokens and why do they affect cost?

A token is the basic unit of text an LLM reads and writes. It can be a whole word, a fragment of a word, a punctuation mark or a single character, depending on the model's tokenization process. The word 'capital', for example, may split into more than one token.

For developers, tokens matter in three concrete ways:

  1. Context window: everything you send, including examples and history, consumes space measured in tokens.2. API pricing: providers bill per million tokens, and output tokens cost more than input tokens; current rates are published on provider pricing pages such as Google AI for Developers and the OpenAI platform.3. Response limits: you can cap the maximum number of generated tokens to control both cost and answer length.

One caution from the video's author, presented as his own account rather than verified reporting: he cites a roughly 190 million dollar figure for training a frontier model. That number is a speaker anecdote about why model development is expensive, not a documented benchmark. What is documented and directly relevant to you is that output tokens are billed at a higher rate, so trimming verbose prompts and capping response length is where your savings come from.

How do LLMs predict answers probabilistically?

An LLM generates text by predicting the next token based on a probability distribution over its vocabulary, conditioned on everything in the context. Given the sentence 'the capital of Brazil is', the model weighs candidates and assigns the highest probability to 'Brasília', completing with it rather than with a low-probability alternative.

This is probabilistic, not deterministic, and it is where the transformer's self-attention mechanism comes in: the model does not look only at the last word, it attends over the whole context you sent and decides which parts carry more weight for the completion. Understanding this single mechanism explains most prompt engineering advice. Clear context raises the probability of relevant tokens; conflicting instructions spread probability across interpretations and produce mushy answers.

How do temperature, Top K and Top P control generation?

Generation parameters let you shape that probability distribution through the API instead of rewriting the prompt. They are exposed in tools like the OpenAI playground, Anthropic console, Google AI Studio and self-hosted frontends such as Open WebUI, which the video's author uses live with a Gemini Flash model to demonstrate the effect.

ParameterWhat it controlsLow value effectHigh value effectTypical use
TemperatureFlattens or sharpens the probability distributionMore deterministic, predictable answersMore creative, varied, sometimes nonsensical outputLow for code and facts; high for brainstorming
Top KHow many candidate tokens are keptKeeps only the strongest few optionsKeeps many options, more varietyRarely tuned; smaller for factual tasks
Top PKeeps tokens until cumulative probability reaches the cutoffAggressive cutoff, only the most likely tokens surviveWider candidate poolThe usual alternative to Top K in hosted APIs

The interaction is worth noting: if you set Top P to a strict cutoff, fewer candidates survive and even a high temperature has less to work with. The video demonstrates this by raising temperature to 2 with a playful system prompt; the model answers the capital-of-Brazil question with a joke about 'saudade' instead of Brasília. That is the parameter doing its job, alongside the system prompt steering tone.

Other controls in the same family include stop sequences, maximum output tokens, and repetition penalties. Each one is a lever on generation; none of them fixes a badly specified task.

Why fundamentals beat copying other people's prompts

Copying someone else's prompt works only by accident, because you do not know their model, their constraints or why each line exists. If you understand personas, reasoning cues, constraints, examples, tokenization and generation parameters, you can read any prompt, see what it is doing, and write a better one for your case.

That is the real payoff of learning prompt engineering fundamentals: new formats, frameworks and 'perfect prompt' trends stop being disruptions and become variations on things you already do. When you need a production-grade setup, let the model draft the prompt, keep PRICE as your checklist, and reserve frameworks like DSPy for code where prompts must be maintained and optimized.

A note on the ecosystem around this material: Brazilian developer education communities actively teach these practices, including Crazystack Typescript (crazystack.com.br), the Bootcamp do Dev Doido, and creators such as Gustavo Dev Doido, alongside Rocketseat, the channel behind the source video for this article.

FAQ

  • Do XML tags make prompts better than Markdown or JSON? No single format is universally better. What matters is clearly separating roles, instructions, context and examples. Use the format your target model's documentation recommends and stay consistent inside one prompt.
  • What does temperature do in an LLM API? Temperature shapes how flat the next-token probability distribution is. Values near zero make output more deterministic and factual; higher values increase variety and creativity at the cost of predictability.
  • What is the PRICE method for prompts? PRICE is a checklist: Persona, Reasoning, Instructions, Constraints, Examples or output format. It ensures a prompt specifies who the model is, how to think, what to do, what to avoid, and what shape the answer must take.
  • Can an LLM write its own prompts? Yes. Ask the model to create a prompt for your task, targeted at the specific model that will run it and following that model's best practices. You still need fundamentals to evaluate and refine the result.
  • Is DSPy a replacement for writing prompts? DSPy replaces hand-written prompt strings with declarative Python modules whose prompts are compiled and can be optimized against a metric. It suits product code where prompts must be maintained, not casual chat usage.

Source video