跪拜 Guibai
← All articles
LLM · Agent · LangChain

LangChain Agent Builder's Guide: From create_agent to Streaming Structured Output

By 东东吖 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

LangChain's create_agent bundles the entire tool-calling loop into a single API, which cuts boilerplate for production assistants. The structured output retry and streaming snapshot behavior are the kind of details that determine whether a typed response pipeline works reliably outside a demo.

Summary

LangChain's official Agent definition is "model + Harness," where Harness covers everything outside the model loop: prompts, tools, middleware, and state. create_agent is the minimal Harness implementation, letting a developer wire up a working assistant with a few lines. The internal loop runs user input to model decision, optional tool calls, result return, and final reply, all managed without hand-written while loops or manual ToolMessage construction.

The note walks through three practical layers. Tools are defined with the @tool decorator, where docstrings and parameter names become part of the model's visible prompt. Runtime-only data like user IDs goes through a ToolRuntime parameter that stays out of the tool schema. For structured output, ToolStrategy wraps a Pydantic schema as a hidden structured-output tool, with built-in retry on validation failures. Streaming uses stream_events, with projections for message text, toolCalls, and tool lifecycle, but structured output streams arrive as full snapshots rather than deltas, so frontend code must check field completeness before rendering.

A complete example combines DeepSeek via an OpenAI-compatible base URL, two tools, a four-field WeatherAnswer schema, and streaming. The author also recommends a learning path: get the minimal agent running, inspect LangSmith trace trees, add ToolStrategy, switch to stream_events, then combine them.

Takeaways
— LangChain defines an Agent as model + Harness; create_agent is the minimal Harness implementation, handling the full tool-calling loop internally.
— Model identifiers in create_agent accept formats like openai:gpt-5.5, anthropic:claude-sonnet-4-6, google_genai:gemini-2.5-flash-lite, and ollama:devstral-2.
— Tool docstrings and parameter names feed directly into the model's prompt, so they must accurately describe what the tool does.
— ToolRuntime parameters get injected during execution and are omitted from the tool schema the model sees.
— ToolStrategy implements structured output through a hidden structured-output tool; validation errors trigger automatic retries without manual try/except.
— In streaming, structured output chunks are complete snapshots, not incremental deltas, so tool call parameters may be partially filled during streaming.
— The complete example passes extra_body={"thinking": {"type": "disabled"}} to DeepSeek because thinking mode does not support the forced tool_choice needed for structured output.
Conclusions

create_agent's abstraction hides the tool-calling loop so completely that a developer can ship a working agent without understanding LangGraph, but debugging still requires LangSmith trace trees to see the loop's intermediate nodes.

The decision to expose ToolRuntime as a separate parameter is a clean solution to a real problem: keeping secrets and session state out of the model's context window while still letting tools access them.

Streaming structured output as snapshots rather than deltas is a deliberate trade-off: it simplifies backend implementation but pushes completeness checks to the frontend, turning a streaming API into a polling-like pattern for typed data.

Concepts & terms
Harness
The execution framework around the language model: prompts, tools, middleware, state management, and the loop that routes model outputs back into tool calls. LangChain positions create_agent as a minimal Harness implementation.
ToolStrategy
A structured output strategy for LangChain Agents that wraps a Pydantic schema as a hidden tool. The model calls this tool and its arguments become the typed structured response, with automatic retries on validation failures.
stram_events (v3)
The streaming API for LangChain Agents built on LangGraph. It exposes projections like message.text for token-level output, message.toolCalls for tool argument increments, and stream.output for the final agent state.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗