How to Build an AI Agent from Scratch Using LangChain and OpenAI

Build a working AI agent in Python using LangChain and OpenAI's function calling API. This tutorial covers tool creation, agent loops, memory, error handling, and deployment tips with runnable code.

April 27, 2026

An AI agent is a model-driven program that can choose tools, call them, inspect the results, and continue until it reaches a defined stopping condition. A practical LangChain agent starts with a small set of typed tools, a model, clear instructions, and tests for unsafe or incomplete tool use.

Start with one useful agent task

Choose a task that needs a model to decide among actions. A support agent might look up an order, check a policy, and draft a reply. A calculation agent might call a calculator and return the result with the inputs shown. A single prompt is enough for a fixed transformation, so do not add an agent loop until tool choice or multi-step work is genuinely required.

LangChain's current agent API uses create_agent to connect a model and tools. The model provider is selected through the model identifier or a provider-specific model object. The LangChain agents documentation shows the current tool-calling and invocation patterns.

For a project that needs a larger workflow around this pattern, see AI agent development as the related service context.

Define narrow tools with safe inputs

A tool is a function the agent may call. Give it a narrow purpose, typed arguments, a useful description, and a predictable return value. Keep authentication, authorization, validation, rate limits, and side effects outside the model's control. The model can request an action, but application code must decide if that action is allowed.

from langchain.agents import create_agent; from langchain.tools import tool; @tool def calculate(expression: str) -> str: 'Evaluate an approved arithmetic expression.'; return safe_calculator(expression); agent = create_agent(model='openai:gpt-5.5', tools=[calculate], system_prompt='Use the calculator for arithmetic and explain the result.')

The function name and description are part of the agent interface. A vague description leads to poor tool selection. A tool that accepts an unrestricted command, database query, or URL creates a larger security problem than the agent solves.

Understand the agent loop

The agent receives a message, decides to answer or call a tool, receives the tool result, and repeats until it produces a final response or reaches a stop condition. Put a maximum step count and a request timeout around the run. Log tool names, validation outcomes, latency, and final status so a failed run can be replayed without exposing secrets.

OpenAI's practical agent guide describes a run as a loop with exit conditions such as a tool call, structured final output, an error, or a maximum number of turns. Its advice to define clear actions and capture edge cases applies to a small LangChain agent as well as a larger workflow. Read the agent design guide when turning a prototype into an operational flow.

Add state only when the task needs it

A stateless invocation is easier to reason about. Add conversation history when later turns depend on earlier messages, and add durable state when a run must pause, resume, or survive a process restart. Store a stable thread identifier and define which fields are user input, tool output, approval state, and system-owned data.

Do not place secrets or unrestricted tool results into a long-lived memory store by default. Set retention rules, redact sensitive fields, and test that one user's thread cannot be read by another user. If the agent needs current business data, retrieve it through an authorized tool or RAG layer instead of trusting old conversation text.

Guard the boundaries around model decisions

  • Validate arguments: reject malformed IDs, unexpected file paths, unsafe expressions, and inputs outside the tool's allowed range.
  • Require approval: pause before sending money, deleting records, publishing content, or contacting a third party.
  • Limit access: issue the narrowest credentials and enforce permissions in the tool implementation.
  • Control loops: cap iterations, token use, wall-clock time, and repeated calls to the same tool.
  • Handle missing evidence: let the agent say it cannot answer when a tool returns no usable result.

These controls belong in application code and infrastructure. A system prompt can explain a rule, but it cannot replace an authorization check.

Evaluate the agent with real task traces

Build a test set that covers ordinary requests, ambiguous wording, missing records, tool errors, prompt injection, permission boundaries, and actions that need approval. Measure task success, correct tool selection, argument validity, response quality, latency, and cost. Save representative traces with sensitive values removed.

Start with a capable model to establish a baseline, then test smaller models against the same cases. A cheaper model is useful only if its tool choices and failure behavior remain acceptable. Treat model, prompt, tool schema, and dependency changes as versioned changes to the agent.

Know when a simple chain is better

Use a normal chain for a fixed sequence such as classify, extract, and format. Use an agent when the next step depends on the current result or the user request. The extra flexibility brings extra latency, cost, and failure modes, so the smallest architecture that passes the evaluation set is usually the easiest one to operate.

Building an AI agent from scratch is less about writing a long prompt and more about designing a safe action boundary. Keep tools narrow, give the model clear choices, cap the loop, and test the exact situations that can cause harm.

Run a staged first release

Start with read-only tools and a small set of representative tasks. Capture traces, review every tool call, and add approval gates before introducing side effects. A staged release gives the team evidence about tool selection and failure handling before a model can change business data.

When the agent needs backend integrations and controlled service calls, an AI API development engagement can cover the application boundary around the model.

Found this helpful?

Share this page with others