Skip to content
Back to the blog
Prompt engineering Martin Pohl

Prompt optimization: how to shorten tokens and improve the result

Concrete prompt-optimization techniques: structured prompts, few-shot pruning, prompt compression. With before/after examples and a measurable effect on price and quality.

Sdílet:

A good prompt is short, clear and deterministic. A bad prompt is long, vague and expensive. Here are 6 ways to shorten prompts by 30–60% and often improve the quality at the same time.

1. Instructions first, context after

Models pay attention mostly to the start and the end of a prompt. Put the main instruction first, then the data, then a restatement of the task. Better follow rate, fewer retries, lower cost.

2. Few-shot pruning

Start with 5 examples, remove them one by one and measure quality. You often find that 2 examples are enough. That saves thousands of tokens per request.

Before: 5 examples × 200 tokens = 1000 tokens

After: 2 examples × 200 tokens = 400 tokens → −60% input

3. Structured JSON output

Instead of “explain and return the answer”, require a strict schema. Use response_format: json_schema. Fewer output tokens, a parseable result, deterministic behavior.

4. Abbreviations and symbols

Models understand "Q:" and "A:", "USR:" and "AST:", "→" instead of "results in". In a system prompt or in few-shot examples that saves tens of tokens.

5. Prompt compression tools

Libraries such as LLMLingua can reduce a prompt to 30–50% of its original length with little quality loss. For large RAG contexts they are hard to skip.

6. Drop “please be polite”

"You are a helpful assistant. Please carefully consider..." does not improve anything, and it costs tokens. Be direct: "Classify the text as one of: A, B, C. Output only the letter."

Make it measurable: test, do not guess

Run every prompt change through a test set (50–200 examples) and compare:

  • Accuracy — does quality hold?
  • Avg tokens in/out — what did you actually save?
  • Cost per request — the product of the two above

Without measurement, prompt engineering is superstition. Watch the real model cost across experiments in Tokeny, or estimate it first in the calculator.

A real example

Classifying 50k support tickets a month:

  • Before: GPT-4o, 1200 in / 150 out, $0.18/request → $9,000/month
  • After: GPT-4o mini + structured JSON + 2 few-shot, 400 in / 5 out → $0.062 per 1,000 tickets = $3.1/month

2,900× cheaper. No magic. A smaller model, a shorter prompt and a strict output.

Related: 10 ways to cut AI costs · Token window strategy

Related guides and price pages

LLM Cost Calculator

Estimate across 11 models

GPT API cost

Per-token prices for OpenAI models

Claude API cost

Haiku, Sonnet, Opus pricing

How to calculate LLM cost

The formula, step by step

10 ways to save

Caching, routing, batch APIs

Prompt optimization

Cut tokens by 30–60%

Token window strategy

RAG, sliding window, pruning

Track real AI spend, not just estimates

Upload a CSV from Cursor, Grok Bot or OpenRouter and see daily spend in USD and CZK. The account is free and does not ask for a card.

Sdílet:

Written after hours. If this helped, support my work with a coffee ☕