Most people write prompts by trial and error. You write a prompt, look at the output, change some words, and try again. When the output is wrong, it is hard to know which part of the prompt is the problem. Is the model ignoring your instruction? Is one example confusing it? Is a long paragraph doing anything at all?
For classic machine learning models, we have tools that answer this type of question. They show which parts of the input were most important for a prediction. In this project, we built a similar tool for prompts. PromptExp tells you how much each part of a prompt influences the output of a language model, at the level of detail that you choose.
This post is based on our paper PromptExp: Multi-granularity Prompt Explanation of Large Language Models, written with Ximing Dong, Shaowei Wang, Gopi Krishnan Rajbahadur, and Ahmed Hassan. The paper has the full method, setup, and results.
Why existing tools do not fit
Explanation methods such as Integrated Gradients were designed for models that give one output, for example one class label. A language model gives a sequence of tokens. It produces them one at a time, and each new token depends on the prompt and on all previous tokens. So there is no single output to explain.
You can also ask the model to explain its own answer, for example with chain-of-thought prompting. But those explanations can be hallucinated, and they do not tell you which parts of the prompt the model actually used.
How PromptExp works
PromptExp first gives an importance score to each token in the prompt. Then it adds up the token scores into larger units. We tried two families of methods for the token scores.
Aggregation-based methods
A language model generates the output in rounds, one token per round. In each round, we can use an existing method such as Integrated Gradients to score each prompt token. Then we combine the scores from all rounds with a weighted sum. We tried two weightings. AggEqu samples rounds evenly and gives them equal weight. AggConf gives more weight to rounds where the model was more confident about the token it produced. These methods need access to the model’s internals.
Perturbation-based methods
These methods treat the model as a black box. We generate a baseline output from the original prompt. Then we hide one prompt token, generate a new output, and measure how much the output changed. A large change means the token was important. We measured the change in three ways:
- PerbLog compares the probabilities of the baseline output tokens before and after the change.
- PerbSim compares the meaning of the two outputs with Sentence-BERT embeddings.
- PerbDis counts how many tokens the two outputs share. It is the simplest method.
Choosing the level of detail
Token scores are useful, but developers usually think about a prompt in larger parts. PromptExp adds up the token scores into any unit that you define: words, sentences, or named components such as instruction, example 1, and example 2. This way, the explanation matches how you think about the structure of your prompt.
How well it works
We evaluated PromptExp on sentiment classification with GPT-3.5 and Llama-2. A good explanation should find the tokens that really matter. So we changed the tokens that PromptExp marked as important and checked how often the model’s answer changed.
PerbSim was the best method in all settings. On Llama-2, when we changed the important tokens, the label changed 68% of the time. When we changed the unimportant tokens, it changed only 29% of the time. PerbDis was the weakest, because counting shared tokens misses changes in meaning. PerbLog was weaker on GPT-3.5, because the API shows only the probabilities of the top 20 tokens. With more access to probabilities, PerbLog improved. More sampled rounds helped AggEqu, but AggConf already worked well with only the most confident rounds.
What developers thought
We also asked 10 industry developers to use PromptExp on three tasks.
- Judging the explanations. Developers rated the explanations as reasonable or accurate in 84% of cases (4.2 out of 5 on average). For the prompt “What does AC stand for? Explain like I am five,” PromptExp correctly marked “AC” and “five” as the most important words.
- Understanding wrong answers. 65% found the explanations helpful (3.82 out of 5). In one case, a review said that a movie was a “fun movie” but also “I do not like it.” The model labeled it as positive. PromptExp showed that the model gave a lot of weight to “fun movie” and almost none to “I do not like it.”
- Making prompts shorter. 80% of developers used the scores to remove parts of the prompt with low importance and still kept the behavior they wanted (4.2 out of 5). Shorter prompts cost less.
Practical notes
Explanations have a cost. Perturbation methods need one extra model run for each token, but these runs can be done in parallel. In our setup, a 50-token prompt took about 7 seconds to explain, compared with 4.74 seconds for a normal inference.
Our recommendation: use PerbSim if you want the best accuracy or if you use a closed model through an API. Use AggConf if you have access to the model internals and want lower overhead. If you cannot access probabilities, use PerbSim or PerbDis.
Takeaways
Prompt engineering does not have to be guesswork. If you can see which parts of a prompt the model uses, you can fix wrong answers faster, remove text that does nothing, and check that the model follows your instructions. PromptExp gives this view for both open and closed models, at the level of detail that you need.
