Skip to main content

Command Palette

Search for a command to run...

LangChain Basics: LLM's and Agents

Updated
5 min read
LangChain Basics: LLM's and Agents

What Is LangChain?

LangChain is a framework that helps developers build applications powered by Large Language Models (LLMs) in a structured, controllable, and reliable way.

By default, LLMs are stateless text generators. LangChain enhances them by enabling:

  • Conversation memory

  • Access to external tools (APIs, files, databases)

  • Structured reasoning and execution flows

LangChain does not replace LLMs. Instead, it provides an orchestration layer that guides how LLMs reason, access tools, and generate responses.


What Are LLMs?

LLM (Large Language Model) refers to AI models trained on massive datasets to understand and generate human-like text.

LLMs generate responses based on:

  • The user’s input (prompt)

  • Context provided during the interaction

  • Patterns learned during training

On their own, LLMs do not have memory or decision-making capabilities. LangChain adds these capabilities by injecting structured context and tool access into the model’s workflow.


How LangChain Changes the Interaction Flow

Without LangChain

User → Instructions → LLM → Output

With LangChain

User → Instructions + Context + Memory
     → LLM → Reasoning → Tools / Data
     → Final Output

Instead of responding to a single instruction, the LLM now receives:

  • Relevant context

  • Conversation history

  • Tool descriptions

Based on this information, the model can decide when to call a tool and how to use its output. This is what transforms a simple LLM into an agent.


Installation

pip install -U langchain
uv add langchain

To integrate different model providers:

pip install -U langchain-openai langchain-anthropic langchain-google-genai
uv add langchain-openai langchain-anthropic langchain-google-genai

Prompts in LangChain

A prompt is the input provided to an LLM. LangChain improves prompt handling by allowing prompts to be:

  • Structured and reusable

  • Parameterized with dynamic data

  • Combined with memory and tool outputs

This leads to more consistent and predictable model behavior compared to unstructured prompts.


Chains

Chains allow a task to be broken down into multiple logical steps, executed in sequence.

Example chain flow:

  1. Understand the user’s question

  2. Retrieve relevant information

  3. Summarize the results

Chains make complex workflows easier to manage and reason about.


Agents

LangChain supports multiple types of agents, such as:

  • Basic agents

  • Rule-based agents

  • Multi-agent systems

This blog focuses on basic agents.

What Is a Basic Agent?

A basic agent is an LLM connected to tools that can:

  • Decide which tool to use

  • Execute the tool

  • Use the tool’s output as context

  • Generate a final response

The key difference between a chatbot and an agent is action. Agents do not just respond—they act.


Creating a Basic Agent

from langchain.agents import create_agent

agent = create_agent(
    model="model_name",
    tools=[],
    system_prompt=""
)

Parameters Explained

  • model: The LLM being used (for example, GPT or Gemini)

  • tools: Functions that provide access to external data or actions

  • system_prompt: High-level instructions defining agent behavior


Example Tool: Weather Function

def get_weather(city: str) -> str:
    """Get weather for a city"""
    return f"Weather in {city} is sunny"
agent = create_agent(
    model="gpt-5",
    tools=[get_weather],
    system_prompt="You are an agent that provides accurate information"
)

When the agent receives a query, it evaluates whether a tool is required. If so, it calls the appropriate tool and incorporates the result into its response.


Running the Agent

Correct Ways to Invoke the Agent

# Method 1
agent.invoke({
    "message": [
        {"role": "user", "content": "What is the weather in Pune today?"}
    ]
})

# Method 2
agent.invoke({
    "message": "What is the weather in Pune today?"
})

Incorrect Way

agent.invoke("What is the weather in Pune today?")

Tool Calls Explained

A tool call occurs when the LLM decides to execute a function exposed as a tool.

The tool’s output is then injected back into the prompt as context, enabling the LLM to produce an informed response.

How Does the LLM Choose the Right Tool?

The function’s docstring describes what the tool does.

"""Get weather for a city"""

Clear and specific docstrings help the LLM understand:

  • The purpose of the tool

  • When it should be used

Well-defined tools are critical for reliable agent behavior.


Conclusion

LangChain enables LLMs to move beyond simple prompt–response interactions and operate as intelligent agents capable of reasoning, using tools, and maintaining context.

By structuring prompts, chaining tasks, and enabling tool-based actions, LangChain makes AI applications more predictable, scalable, and production-ready. Rather than relying on raw model responses, developers can now build systems where LLMs know when to think, when to act, and how to combine results into meaningful outputs.

For anyone building real-world AI applications, LangChain provides the foundation for turning language models into truly useful systems.