LLM 消息协议入门:系统提示、推理令牌与工具调用
本文是 AI Hero 系列的基础篇,讲解与大模型对话时消息层的基本协议。作者用简洁图示拆解了几类消息:用户发起的 user message、模型返回的 assistant message,以及位于对话最前、用户通常不可见的 system prompt。关键论断是:当系统提示与用户指令冲突时,模型通常优先服从系统提示,但这一规则可被越狱打破。文章还介绍了属于 assistant message 一部分的 reasoning token,说明消息可包含多部分,例如在图片生成场景中传递文件;最后用一个写 todo.md 的例子解释了工具调用(tool call)与工具结果(tool result)如何通过同一 id 配对,以及这为何能支撑 Claude Code、Cursor 一类的应用在本地系统上操作。适合刚接触 LLM 应用开发、想理清消息结构的工程师快速入门。
The first part of understanding LLMs is understanding the protocol for talking to them - the messages that make up the conversation.
What is a user message? What is a system prompt? What are tool calls? What are reasoning tokens? Let's break it down now.
理解 LLM 的第一步,是弄懂与它们对话的协议——也就是构成这段对话的消息。
什么是用户消息?什么是系统提示?什么是工具调用?什么是推理 token?我们现在就来逐一拆解。
A simple conversation with an LLM might look like this. You have user messages, which are messages from you, the user. LLM messages are called assistant messages.
In this situation we're just talking to the bare model that the model provider gives us.
和 LLM 的简单对话大概是这样的:有来自你(用户)的用户消息;LLM 发出的消息则称为助手消息。
在这种情况里,我们只是在和模型提供方给我们的裸模型直接对话。
But if we want to customize the LLM's behavior, then we can use a system prompt.
System prompts are messages at the very start of the history, which are visible to the LLM, but usually not to the user.
They're powerful, too. The LLM, if there's a conflict between the user and the system prompt, will usually obey the system prompt. For instance, we can tell it to respond in Morse code, and then even if we say stop doing Morse code, it will reply in Morse code.
Of course, this doesn't always work and there's plenty of examples of people jailbreaking the system prompt, but this is at least the theory.
但如果想定制 LLM 的行为,就可以使用系统提示(system prompt)。
系统提示位于消息历史的最开头,LLM 能看到,但用户通常看不到。
系统提示的威力也很大。当用户消息和系统提示冲突时,LLM 通常会服从系统提示。比如,我们可以让它用摩斯码回复;即使后续再说“别用摩斯码了”,它仍然会用摩斯码回应。
当然,这并不总是奏效,网上也不乏“越狱”系统提示的例子;但至少在理论上是这样。
Lots of models can send back reasoning tokens, where the model appears to think through its output before it responds. These reasoning tokens are just another part of the assistant message.
很多模型会返回推理 token(reasoning tokens)——在真正回应之前,模型看起来会先把输出“想”一遍。这些推理 token 只是助手消息的另一个组成部分。
This is an important concept here that messages can contain multiple parts. This is useful for file parts too, where you can send files to LLMs or get files back from them in image generation use cases.
For instance here, we're sending a file to an LLM saying summarize this PDF, and it replies with a summary.
这里有个重要概念:一条消息可以包含多个部分。这对文件部分也很有用——你可以把文件发给 LLM,或在图片生成场景中从 LLM 那里拿回文件。
比如这里,我们把一个文件发给 LLM,让它总结这份 PDF,它就会返回一份摘要。
With tools you have the ability to say you have access to a write file tool to the LLM. This might write a file to our file system or something.
Then the user is going to say, write a todo.md file. The assistant then produces a tool call message. That tool call has an id on it. And it also has an instruction to write a file todo.md with empty contents.
Now we, in our applications, take that tool call and execute it, and we send back to the LLM a tool result with the same id as the tool call and the result of what happened, usually as a string.
We get back from the assistant a summary of what happened, "wrote a todo.md file successfully".
接着,我们在自己的应用里接收这条工具调用并执行它,然后把工具结果回传给 LLM——这个结果带有与工具调用相同的 id,内容是实际发生的结果,通常是一个字符串。
我们会从助手那里收到一句总结:“已成功写入 todo.md 文件”。
So tools are kind of like a conversation between the LLM and our system, where:
The tool call is the LLM asking us for something
The tool result is us giving it the information it needs
That's how AI-powered apps like Claude Code and Cursor can do things on your system. We'll dig deeper into tools later.
所以,工具有点像 LLM 和我们系统之间的一场对话:
工具调用,是 LLM 在向我们请求某样东西;
工具结果,是我们把它需要的信息交给它。
Claude Code、Cursor 这类 AI 应用,正是靠这种方式在你的系统上动手做事。关于工具,我们后面会再深入讲。