Skip to main content

Config-driven LangChain create_agent wrapper

Project description

lc-agent-factory

A thin, config-driven wrapper around LangChain's create_agent.

Our create_agent(config, **kwargs) accepts an additional config parameter on top of LangChain's create_agent's keyword arguments — letting you declare complex object arguments (e.g. model, middleware, tools) in a plain dict or external file, instead of wiring them in code. The returned agent is exactly what create_agent returns. No magic, no lock-in.

Note: Not all create_agent parameters are configurable yet — pass unsupported ones directly as kwargs. When a parameter appears in both, kwargs take priority for scalar values, while lists are merged.

from lc_agent_factory import create_agent

agent = create_agent(config)  # config is just a dict

create_agent() accepts a plain dict. Load it however fits your stack — YAML file, JSON, environment variable, database, or constructed directly in code.


Install

pip install lc-agent-factory

Then install your LLM provider:

pip install langchain-openai        # OpenAI
pip install langchain-anthropic     # Anthropic
pip install langchain-google-genai  # Google GenAI
# see https://python.langchain.com/docs/integrations/chat/ for all providers

Quickstart

1. Create a config file:

# config.yaml
models:
    primary:
        model_provider: google_genai
        model: gemini-2.5-flash
        temperature: 0

middleware:
    prebuilt:
        - ModelCallLimitMiddleware:
              run_limit: 10
              exit_behavior: 'end'

2. Set your API key:

export GOOGLE_API_KEY="..."

3. Run:

import yaml
from lc_agent_factory import create_agent

agent = create_agent(yaml.safe_load(open("config.yaml")))

res = agent.invoke({"messages": [ {"role": "user", "content": "Hello!"} ]})
print(res["messages"][-1].content)

Configuration reference

globals (optional)

globals:
    set_debug: false # default: false
    set_verbose: false # default: false

models

Named model configurations. primary is required. Any additional models (e.g. fallback) are referenced by name in middleware.

models:
    primary:
        model_provider: openai # any init_chat_model provider
        model: gpt-4o
        temperature: 0
        timeout: 60

    fallback:
        model_provider: openai
        model: gpt-4o-mini
        temperature: 0

All keys under a model entry are passed as-is to LangChain's init_chat_model — refer to its docs for available options per provider.

middleware.prebuilt (optional)

A list of LangChain built-in middleware. Each entry is a single-key dict: {MiddlewareClassName: {kwargs}}.

middleware:
    prebuilt:
        # Stop after N model calls
        - ModelCallLimitMiddleware:
              run_limit: 10
              exit_behavior: 'end'

        # Stop after N total tool calls
        - ToolCallLimitMiddleware:
              run_limit: 20

        # Per-tool call limit
        - ToolCallLimitMiddleware:
              tool_name: 'web_search'
              run_limit: 5

        # Retry failed model calls
        - ModelRetryMiddleware:
              max_retries: 3
              backoff_factor: 2.0
              initial_delay: 1.0

        # Retry failed tool calls
        - ToolRetryMiddleware:
              max_retries: 2
              backoff_factor: 2.0
              initial_delay: 1.0

        # Automatically switch to fallback model on failure
        - ModelFallbackMiddleware:
              first_model: 'fallback'

        # Trim old tool messages when context grows too large
        - ContextEditingMiddleware:
              token_count_method: 'approximate'
              edits:
                  - ClearToolUsesEdit:
                        trigger: 50000
                        keep: 3
                        clear_tool_inputs: false
                        placeholder: '[cleared]'

Kwargs are passed as-is to LangChain middleware constructors — refer to LangChain middleware docs for the full list and options.

middleware.custom (optional)

Same structure as middleware.prebuilt, for your own AgentMiddleware subclasses.


Adding tools

Pass tools at call time — they are merged and deduplicated with any tools returned by internal loaders:

from langchain.tools import tool

@tool
def web_search(query: str) -> str:
    """Search the web."""
    ...

agent = create_agent(config, tools=[web_search])

Async

The returned agent is a compiled LangGraph and supports all four invocation modes out of the box:

# sync
agent.invoke({"messages": [...]})

# async
await agent.ainvoke({"messages": [...]})

# streaming
for chunk in agent.stream({"messages": [...]}):
    print(chunk)

# async streaming
async for chunk in agent.astream({"messages": [...])):
    print(chunk)

License

MIT — free to use for any purpose, personal or commercial. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lc_agent_factory-0.1.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lc_agent_factory-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file lc_agent_factory-0.1.0.tar.gz.

File metadata

  • Download URL: lc_agent_factory-0.1.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.14 HTTPX/0.28.1

File hashes

Hashes for lc_agent_factory-0.1.0.tar.gz
Algorithm Hash digest
SHA256 954ac0ca2f72803f20f37f85fc213452cc0e825862e5b948fddead3ec9043ad9
MD5 626176a946dc75ab38d97bd78530bb30
BLAKE2b-256 f5e3beec7315a712e94fcb124a4591af7fd61571d03f757c9c3b7f7394363163

See more details on using hashes here.

File details

Details for the file lc_agent_factory-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lc_agent_factory-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.14 HTTPX/0.28.1

File hashes

Hashes for lc_agent_factory-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5656c62351f57e7a989445f2e2e04b7f3ddae41b3b23bc89639d47b4bbc586a0
MD5 e99e17bf293cd242b632ec8231df237f
BLAKE2b-256 7f1a54e6266813eb4253a7c7df3a3b9c5bee929ac7b8400d72382abc2acf6acd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page