How to build an agent with LangChain
Create a focused tool-calling agent, give it a safe tool, and run it with clear operational limits.
8 min read·July 20, 2026
Define one typed tool
A good first agent has a narrow job. Make tool inputs explicit and keep credentials inside the tool implementation rather than putting them in prompts.
from langchain.tools import tool
@tool
def lookup_invoice(invoice_id: str) -> str:
"""Return the status and balance for an invoice."""
return billing_api.get_invoice(invoice_id)Create and invoke the agent
Bind the tool to a supported chat model and give the agent a short system instruction that describes success and important boundaries.
from langchain.agents import create_agent
agent = create_agent(
model="openai:gpt-5",
tools=[lookup_invoice],
system_prompt="Help finance staff inspect invoices. Never modify billing data.",
)
result = agent.invoke({
"messages": [{"role": "user", "content": "Check invoice inv_1042"}]
})Make it production-ready
Add tracing, retries around transient failures, a maximum step count, and authorization outside the model. Test normal requests as well as prompt injection and malformed tool arguments.