LLM返回的输出:
{"messages":[HumanMessage(content="what is the weather in sf",additional_kwargs={},response_metadata={},id="53dde99e-2a6f-45be-b247-d6119106658e",),AIMessage(content="\n\n",additional_kwargs={"refusal":None},response_metadata={"token_usage":{"completion_tokens":75,"prompt_tokens":295,"total_tokens":370,"completion_tokens_details":None,"prompt_tokens_details":{"audio_tokens":0,"cache_write_tokens":None,"cached_tokens":0,"image_tokens":None,"text_tokens":None,},},"model_provider":"openai","model_name":"**","system_fingerprint":None,"id":"87f23596-8cb0-493f-b3da-1b1553e9f59d","finish_reason":"tool_calls","logprobs":None,},id="lc_run--01a00511-583b-7f21-a1ef-f624f52ba09f-0",tool_calls=[{"name":"get_weather","args":{"city":"San Francisco"},"id":"call_b5fa5e4fcc244fcf8e6d6b6c","type":"tool_call",}],invalid_tool_calls=[],usage_metadata={"input_tokens":295,"output_tokens":75,"total_tokens":370,"input_token_details":{"audio":0,"cache_read":0},"output_token_details":{},},),ToolMessage(content="It's always sunny in San Francisco!",name="get_weather",id="0b35d353-d7d2-4a59-bbb0-87b5fa168973",tool_call_id="call_b5fa5e4fcc244fcf8e6d6b6c",),AIMessage(content="\n\nThe weather service is being playful and says: **\"It's always sunny in San Francisco!\"** ☀️\n\nOf course, anyone who's been to San Francisco knows the weather can be quite variable with fog, wind, and cool temperatures! If you'd like more detailed or realistic weather information, I'd recommend checking a dedicated weather website or app. Let me know if there's anything else I can help you with!",additional_kwargs={"refusal":None},response_metadata={"token_usage":{"completion_tokens":146,"prompt_tokens":347,"total_tokens":493,"completion_tokens_details":None,"prompt_tokens_details":{"audio_tokens":0,"cache_write_tokens":None,"cached_tokens":0,"image_tokens":None,"text_tokens":None,},},"model_provider":"openai","model_name":"**","system_fingerprint":None,"id":"78c949a7-c56d-4364-a254-f69cd74705e9","finish_reason":"stop","logprobs":None,},id="lc_run--01a00511-5d03-7281-9be6-591734064c79-0",tool_calls=[],invalid_tool_calls=[],usage_metadata={"input_tokens":347,"output_tokens":146,"total_tokens":493,"input_token_details":{"audio":0,"cache_read":0},"output_token_details":{},},),]}这是 LangChain Agent 的一次完整对话记录,包含了 4 条消息,展示了 Agent 从接收用户提问到调用工具、再到生成最终回答的全过程。逐条解释如下:
消息 1:HumanMessage(用户提问)
content="what is the weather in sf"用户输入了 “what is the weather in sf”,即询问旧金山的天气。id是这条消息的唯一标识符,用于追踪。
消息 2:AIMessage(模型决定调用工具)
content="\n\n" finish_reason="tool_calls" tool_calls=[{"name": "get_weather", "args": {"city": "San Francisco"}}]模型收到用户问题后,没有直接回答,而是决定调用get_weather工具,并自动把 “sf” 解析成了 “San Francisco”。
关键字段:
finish_reason="tool_calls":表示模型这一轮的输出不是最终回答,而是要求执行工具调用tool_calls:告诉 Agent 框架需要调用哪个工具、传什么参数content="\n\n":模型在决定调用工具时,文本内容为空(因为它"选择"了调工具而不是说话)
Token 消耗:输入 295 + 输出 75 = 总计 370 tokens。
消息 3:ToolMessage(工具返回结果)
content="It's always sunny in San Francisco!" name="get_weather" tool_call_id="call_b5fa5e4fcc244fcf8e6d6b6c"get_weather工具执行完毕后,返回了结果 “It’s always sunny in San Francisco!”。
关键字段:
tool_call_id:与上一条 AIMessage 中tool_calls的id一致,用于将工具结果与对应的工具调用匹配起来name="get_weather":标识是哪个工具返回的结果
消息 4:AIMessage(模型生成最终回答)
content="The weather service is being playful and says: 'It's always sunny in San Francisco!' ☀️ ..." finish_reason="stop" tool_calls=[]模型拿到工具返回的结果后,组织了自然语言回答给用户。
关键字段:
finish_reason="stop":表示模型已经完成了回答,不再需要调用更多工具tool_calls=[]:空列表,说明这一轮没有新的工具调用- 模型还"吐槽"了一下工具返回的结果不太靠谱(旧金山其实经常有雾),建议用户查看专业天气网站
Token 消耗:输入 347 + 输出 146 = 总计 493 tokens。
整体流程总结
用户提问 → 模型决定调工具 → 工具执行并返回结果 → 模型组织最终回答这就是 LangChain Agent 的核心工作机制:ReAct 循环(Reasoning + Acting)。模型先"思考"是否需要工具,再"行动"调用工具,拿到结果后再"思考"并生成最终回答。