Home / Function/ create_agent() — langchain Function Reference

create_agent() — langchain Function Reference

Architecture documentation for the create_agent() function in factory.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  f4b66c38_651c_807a_caca_41f73fbbe516["create_agent()"]
  fd7a28b1_3772_169b_6524_1342f35143b1["factory.py"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|defined in| fd7a28b1_3772_169b_6524_1342f35143b1
  0d0f80a1_fa92_e582_4a35_948953b3da45["_chain_tool_call_wrappers()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 0d0f80a1_fa92_e582_4a35_948953b3da45
  655c790d_8e18_faf9_28c0_bb4d668f853a["_chain_async_tool_call_wrappers()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 655c790d_8e18_faf9_28c0_bb4d668f853a
  f3b85e63_c3a1_d200_12a5_4dccafed80ff["_chain_model_call_handlers()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| f3b85e63_c3a1_d200_12a5_4dccafed80ff
  611bf38d_1c0a_8d53_2a61_0d70eb6cac51["_chain_async_model_call_handlers()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 611bf38d_1c0a_8d53_2a61_0d70eb6cac51
  833c00b4_ff65_504a_fd91_d0dd020207ea["_resolve_schema()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 833c00b4_ff65_504a_fd91_d0dd020207ea
  31584a4b_7eff_75cb_ac2d_a6ac7f732e5b["_handle_structured_output_error()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 31584a4b_7eff_75cb_ac2d_a6ac7f732e5b
  415a1029_3a76_d9b4_4470_cf97d4851d11["_supports_provider_strategy()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 415a1029_3a76_d9b4_4470_cf97d4851d11
  75a07fc9_a1f8_f785_227c_5009d6f748b3["_build_commands()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 75a07fc9_a1f8_f785_227c_5009d6f748b3
  876bb633_dd26_1b0a_98e8_1ce27a9ed076["_make_tools_to_model_edge()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 876bb633_dd26_1b0a_98e8_1ce27a9ed076
  20046200_0f69_48b9_d47c_7a3ea7d0358f["_make_model_to_tools_edge()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 20046200_0f69_48b9_d47c_7a3ea7d0358f
  2725013f_5b47_34dc_d7d6_245ddd8cc6ca["_make_model_to_model_edge()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 2725013f_5b47_34dc_d7d6_245ddd8cc6ca
  b82fcc4d_63ed_966a_2618_0b729f787b4e["_add_middleware_edge()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| b82fcc4d_63ed_966a_2618_0b729f787b4e
  13fe0c9b_88b5_8ef8_4de2_a1086d366c27["_get_can_jump_to()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 13fe0c9b_88b5_8ef8_4de2_a1086d366c27
  style f4b66c38_651c_807a_caca_41f73fbbe516 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/factory.py lines 653–1604

def create_agent(
    model: str | BaseChatModel,
    tools: Sequence[BaseTool | Callable[..., Any] | dict[str, Any]] | None = None,
    *,
    system_prompt: str | SystemMessage | None = None,
    middleware: Sequence[AgentMiddleware[StateT_co, ContextT]] = (),
    response_format: ResponseFormat[ResponseT] | type[ResponseT] | dict[str, Any] | None = None,
    state_schema: type[AgentState[ResponseT]] | None = None,
    context_schema: type[ContextT] | None = None,
    checkpointer: Checkpointer | None = None,
    store: BaseStore | None = None,
    interrupt_before: list[str] | None = None,
    interrupt_after: list[str] | None = None,
    debug: bool = False,
    name: str | None = None,
    cache: BaseCache[Any] | None = None,
) -> CompiledStateGraph[
    AgentState[ResponseT], ContextT, _InputAgentState, _OutputAgentState[ResponseT]
]:
    """Creates an agent graph that calls tools in a loop until a stopping condition is met.

    For more details on using `create_agent`,
    visit the [Agents](https://docs.langchain.com/oss/python/langchain/agents) docs.

    Args:
        model: The language model for the agent.

            Can be a string identifier (e.g., `"openai:gpt-4"`) or a direct chat model
            instance (e.g., [`ChatOpenAI`][langchain_openai.ChatOpenAI] or other another
            [LangChain chat model](https://docs.langchain.com/oss/python/integrations/chat)).

            For a full list of supported model strings, see
            [`init_chat_model`][langchain.chat_models.init_chat_model(model_provider)].

            !!! tip ""

                See the [Models](https://docs.langchain.com/oss/python/langchain/models)
                docs for more information.
        tools: A list of tools, `dict`, or `Callable`.

            If `None` or an empty list, the agent will consist of a model node without a
            tool calling loop.


            !!! tip ""

                See the [Tools](https://docs.langchain.com/oss/python/langchain/tools)
                docs for more information.
        system_prompt: An optional system prompt for the LLM.

            Can be a `str` (which will be converted to a `SystemMessage`) or a
            `SystemMessage` instance directly. The system message is added to the
            beginning of the message list when calling the model.
        middleware: A sequence of middleware instances to apply to the agent.

            Middleware can intercept and modify agent behavior at various stages.

            !!! tip ""

                See the [Middleware](https://docs.langchain.com/oss/python/langchain/middleware)
                docs for more information.
        response_format: An optional configuration for structured responses.

            Can be a `ToolStrategy`, `ProviderStrategy`, or a Pydantic model class.

            If provided, the agent will handle structured output during the
            conversation flow.

            Raw schemas will be wrapped in an appropriate strategy based on model
            capabilities.

            !!! tip ""

                See the [Structured output](https://docs.langchain.com/oss/python/langchain/structured-output)
                docs for more information.
        state_schema: An optional `TypedDict` schema that extends `AgentState`.

            When provided, this schema is used instead of `AgentState` as the base
            schema for merging with middleware state schemas. This allows users to
            add custom state fields without needing to create custom middleware.

Domain

Subdomains

Frequently Asked Questions

What does create_agent() do?
create_agent() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/factory.py.
Where is create_agent() defined?
create_agent() is defined in libs/langchain_v1/langchain/agents/factory.py at line 653.
What does create_agent() call?
create_agent() calls 13 function(s): _add_middleware_edge, _build_commands, _chain_async_model_call_handlers, _chain_async_tool_call_wrappers, _chain_model_call_handlers, _chain_tool_call_wrappers, _get_can_jump_to, _handle_structured_output_error, and 5 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free