Skip to main content
BaseAgent is the core building block of the Agentic Patterns library. It wraps a LangChain chat model in a ChatPromptTemplate pipeline and exposes a single invoke() method. All other agents (PlanningAgent, ExecutionAgent, MonitoringAgent) extend this class.

Import

from agents import BaseAgent
# or
from agents.base_agent import BaseAgent

Constructor

BaseAgent(
    llm: BaseChatModel,
    system_prompt: str = "You are a helpful AI assistant.",
    agent_name: str = "BaseAgent"
)
llm
BaseChatModel
required
The LangChain chat model to use for inference. Any model that implements BaseChatModel is accepted (e.g. ChatOllama, ChatOpenAI).
system_prompt
str
default:"You are a helpful AI assistant."
System-level instruction prepended to every conversation turn via ChatPromptTemplate. Override this to give the agent a specific persona or constraints.
agent_name
str
default:"BaseAgent"
Display name used in log messages. Useful for distinguishing agents in multi-agent workflows.

Methods

invoke

invoke(user_input: str, **kwargs: Any) -> Any
Sends user_input through the ChatPromptTemplate | llm chain and returns the model’s text response. Internally the template is:
[("system", system_prompt), ("human", "{input}")]
user_input
str
required
The main task or prompt to send to the model.
**kwargs
Any
Additional key-value pairs merged into the prompt template’s format call.
Returns: str — the response.content string from the model.
If the underlying chain raises an exception the error is logged at ERROR level and re-raised. No silent fallback occurs in BaseAgent.invoke().

Usage example

from langchain_ollama import ChatOllama
from agents import BaseAgent

# Instantiate a local Ollama model
llm = ChatOllama(model="llama3")

# Create a base agent with a custom system prompt
agent = BaseAgent(
    llm=llm,
    system_prompt="You are a concise technical writer.",
    agent_name="WriterAgent"
)

# Invoke the agent
response = agent.invoke("Explain what a REST API is in two sentences.")
print(response)
# → "A REST API is an architectural style for building web services ..."

Build docs developers (and LLMs) love