Skip to main content

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.

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.2.tar.gz (107.4 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.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lc_agent_factory-0.1.2.tar.gz
  • Upload date:
  • Size: 107.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.12

File hashes

Hashes for lc_agent_factory-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fa8b813c5d4271d3e8e6b1789507e2bc975956f91575e72934fc974b51a66361
MD5 e5c7aad6fb0b5611231b38ecf9008960
BLAKE2b-256 8cf21d09a9a4e93acb7c7e964bdaca49272ce8efd0a480314d44c2edbea5465b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lc_agent_factory-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 481c0162b9723ea31cb488e29a0a910fd410d6b6438c842a1b590f3fd9905dfd
MD5 5ddd8f6f91e3ca4e519bdab529879943
BLAKE2b-256 947545858c827435c1af739ee8360b55edf93d2fb07d97266d3ec707d4b94495

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page