{"id":103234,"date":"2026-03-09T17:55:08","date_gmt":"2026-03-09T12:25:08","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=103234"},"modified":"2026-03-11T18:58:49","modified_gmt":"2026-03-11T13:28:49","slug":"building-a-langchain-agent-for-llm-in-python","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/building-a-langchain-agent-for-llm-in-python\/","title":{"rendered":"Building a LangChain Agent for LLM Applications in Python"},"content":{"rendered":"\n<p>Imagine you have a really smart assistant. It knows a lot of things, but it can&#8217;t look anything up on its own. It just answers from memory. That&#8217;s basically what a regular AI model does: it responds based on what it was trained on, but it can&#8217;t go out and fetch new information.<\/p>\n\n\n\n<p>A <strong>LangChain Agent<\/strong> changes that. It gives the AI the ability to actually <em>do things<\/em>, search Wikipedia, run calculations, query databases, read files, and use the results to give you a better answer. Think of it as upgrading your AI from a know-it-all to a know-it-all that can also <em>look things up<\/em>.<\/p>\n\n\n\n<p>In this article, you&#8217;ll build one from scratch. No prior coding experience assumed. Every line of code is explained in plain English so you too can build your own AI agent. So, without further ado, let us get started!<\/p>\n\n\n\n<p><strong>TL\/DR Summary:&nbsp;<\/strong><\/p>\n\n\n\n<p>A LangChain agent is a Python-based AI system that uses a large language model as its brain to reason through problems, pick the right tools, and take action \u2014 all in an automated loop. You can build one in under 50 lines of code using LangChain&#8217;s free, open-source framework.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is LangChain?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"628\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/1-4.png\" alt=\"What is LangChain?\" class=\"wp-image-103673\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/1-4.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/1-4-300x157.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/1-4-768x402.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/1-4-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.langchain.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">LangChain<\/a> is an open-source <a href=\"https:\/\/guvi.in\/hub\/python\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> framework designed specifically for building applications powered by large language models. At its core, it provides a standardized, modular way to connect <a href=\"https:\/\/www.guvi.in\/blog\/guide-to-large-language-models\/\" target=\"_blank\" rel=\"noreferrer noopener\">LLMs<\/a>, think OpenAI&#8217;s GPT, Anthropic&#8217;s Claude, or Google&#8217;s Gemini, to the outside world: external data sources, APIs, databases, and custom tools.<\/p>\n\n\n\n<p>Before LangChain, developers had to write custom integration code for every new model, manage conversation history manually, and figure out their own patterns for chaining model calls. LangChain abstracts all of that away and gives you clean, reusable building blocks.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding a LangChain Agent<\/strong><\/h2>\n\n\n\n<p>So what exactly is a LangChain agent? At its simplest, an agent is a system that uses an LLM as a reasoning engine to decide what actions to take and then executes those actions using tools. Unlike a standard LLM call that gives you a one-shot response, an agent runs in a loop, reasoning, acting, observing, and repeating until it arrives at a final answer.<\/p>\n\n\n\n<p>Here&#8217;s the key distinction you should internalize early: a <strong>chain<\/strong> follows a fixed, predefined sequence of steps. An <strong>agent<\/strong>, on the other hand, decides dynamically which steps to take based on the task at hand. That flexibility is what makes agents so powerful, and also what makes them slightly trickier to design well.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\"><strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong> <br \/><br \/>LangChain is built on top of LangGraph, a lower-level graph-based orchestration runtime. While LangChain is ideal for quickly building agents, LangGraph is better suited for advanced use cases requiring fine-grained control over stateful, multi-step workflows. You don&#8217;t need to know LangGraph to get started with LangChain agents.<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Does a LangChain Agent Actually Work?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"628\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/2-4.png\" alt=\"How Does a LangChain Agent Actually Work?\" class=\"wp-image-103674\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/2-4.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/2-4-300x157.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/2-4-768x402.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/2-4-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>When you ask the agent a question, it doesn&#8217;t just reply immediately. Instead it goes through a loop that looks like this:<\/p>\n\n\n\n<p><em>You ask: &nbsp; &#8220;What is LangChain?&#8221;<\/em><\/p>\n\n\n\n<p><em>Agent thinks: &nbsp; &#8220;I should look this up on Wikipedia.&#8221;<\/em><\/p>\n\n\n\n<p><em>Agent acts: &nbsp; &nbsp; Searches Wikipedia for &#8220;LangChain.&#8221;<\/em><\/p>\n\n\n\n<p><em>Agent observes: Gets back a Wikipedia summary<\/em><\/p>\n\n\n\n<p><em>Agent thinks: &nbsp; &#8220;I now have enough to answer&#8221;<\/em><\/p>\n\n\n\n<p><em>Agent replies:&nbsp; Gives you the final answer<\/em><\/p>\n\n\n\n<p>This loop of <strong>Think \u2192 Act \u2192 Observe \u2192 Repeat<\/strong> is called the <strong>ReAct framework<\/strong>. It&#8217;s what makes agents so much more powerful than a regular AI chat, instead of guessing, the agent actually goes and gets real information before answering.<\/p>\n\n\n\n<p>The whole system has 5 parts working together:<\/p>\n\n\n\n<ul>\n<li><strong>The Brain (LLM):<\/strong> The AI model that does the thinking \u2014 we&#8217;ll use Llama 3.3 via Groq<\/li>\n\n\n\n<li><strong>The Tools:<\/strong> Things the agent can use, we&#8217;ll give it Wikipedia search<\/li>\n\n\n\n<li><strong>The Memory:<\/strong> Lets it remember what was said earlier in the conversation<\/li>\n\n\n\n<li><strong>The Prompt:<\/strong> Instructions that teach it how to think step by step<\/li>\n\n\n\n<li><strong>The Executor:<\/strong> The engine that runs the whole loop<\/li>\n<\/ul>\n\n\n\n<p>If you want to read and learn everything there is to about Python, then consider getting HCL GUVI\u2019s Free <em>\u201c<\/em><a href=\"https:\/\/www.guvi.in\/mlp\/python-ebook?utm_source=blog&amp;utm_medium=hyperlink+&amp;utm_campaign=langchain-agent-for-llm-applications\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Python eBook<\/em><\/a><em>: A Beginner&#8217;s Guide to Coding &amp; Beyond<\/em><a href=\"https:\/\/www.guvi.in\/mlp\/python-ebook?utm_source=blog&amp;utm_medium=hyperlink+&amp;utm_campaign=langchain-agent-for-llm-applications\" target=\"_blank\" rel=\"noreferrer noopener\"><em>.<\/em><\/a><em>\u201d <\/em>In this, you will learn all about OOPs in Python, File handling, &amp; Database Connectivity, along with Python libraries and advanced concepts!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 1: Setting Everything Up<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s get practical. Before you can build your first LangChain agent, you&#8217;ll need to set up your Python environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Check If Python Is Installed<\/strong><\/h3>\n\n\n\n<p>Open your <strong>terminal<\/strong> (Mac\/Linux) or <strong>Command Prompt<\/strong> (Windows) and type:<\/p>\n\n\n\n<p><code>python --version<\/code><\/p>\n\n\n\n<p>You should see something like Python 3.10.x or Python 3.11.x. If you see an error, go to <a href=\"http:\/\/python.org\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><strong>python.org<\/strong>,<\/a> download Python, and install it. Come back here once that&#8217;s done.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Create a Project Folder<\/strong><\/h3>\n\n\n\n<p>Think of this as creating a dedicated drawer for this project so files don&#8217;t get mixed up with everything else on your computer.<\/p>\n\n\n\n<p><code>mkdir langchain_agent<\/code><\/p>\n\n\n\n<p><code>cd langchain_agent<\/code><\/p>\n\n\n\n<p>mkdir creates the folder. cd moves you into it. All your files will live here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Create a Virtual Environment<\/strong><\/h3>\n\n\n\n<p><code>python -m venv langchain_env<\/code><\/p>\n\n\n\n<p><strong>What is this?<\/strong> Think of a virtual environment as a clean, empty room for your project. When you install packages here, they stay in this room and don&#8217;t mess with anything else on your computer. Always a good habit.<\/p>\n\n\n\n<p>Now activate it:<\/p>\n\n\n\n<p><strong>On Mac\/Linux:<\/strong><\/p>\n\n\n\n<p><code>source langchain_env\/bin\/activate<\/code><\/p>\n\n\n\n<p><strong>On Windows:<\/strong><\/p>\n\n\n\n<p><code>langchain_env\\Scripts\\activate<\/code><\/p>\n\n\n\n<p>You&#8217;ll know it worked when you see (langchain_env) appear at the start of your terminal line. That means you&#8217;re now &#8220;inside&#8221; the room.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Install All Required Packages<\/strong><\/h3>\n\n\n\n<p>Copy and paste this entire command, all in one go:<\/p>\n\n\n\n<p><code>pip install langchain==0.3.0 langchain-community==0.3.0 langchain-core==0.3.0 langchain-groq==0.2.0 wikipedia python-dotenv<\/code><\/p>\n\n\n\n<p><strong>What are these?<\/strong><\/p>\n\n\n\n<p>&#8211; `langchain` \u2192 the main framework that ties everything together<\/p>\n\n\n\n<p>&#8211; `langchain-community` \u2192 extra tools like Wikipedia search<\/p>\n\n\n\n<p>&#8211; `langchain-core` \u2192 the engine underneath LangChain<\/p>\n\n\n\n<p>&#8211; `langchain-groq` \u2192 lets LangChain talk to Groq&#8217;s free AI models<\/p>\n\n\n\n<p>&#8211; `wikipedia` \u2192 the Python library that actually fetches Wikipedia results<\/p>\n\n\n\n<p>&#8211; `python-dotenv` \u2192 helps you store your API key safely<\/p>\n\n\n\n<p>This will take a minute or two. You&#8217;ll see a lot of text scrolling, that&#8217;s completely normal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Get Your Free Groq API Key<\/strong><\/h3>\n\n\n\n<p>We&#8217;re using Groq instead of OpenAI because Groq is completely free and doesn&#8217;t require a credit card.<\/p>\n\n\n\n<p>1. Go to <a href=\"http:\/\/console.groq.com\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">console.groq.com<\/a><\/p>\n\n\n\n<p>2. Click Sign Up and create a free account<\/p>\n\n\n\n<p>3. Once logged in, click API Keys in the left sidebar<\/p>\n\n\n\n<p>4. Click Create API Key, give it any name, and copy the key<\/p>\n\n\n\n<p>Your key will look something like this: `gsk_abc123xyz&#8230;`<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 6: Create Your `.env` File<\/strong><\/h3>\n\n\n\n<p>Inside your `langchain_agent` folder, create a new file called exactly `.env`, yes, with the dot at the start, and no other extension. Open it and paste this:<\/p>\n\n\n\n<p><code>GROQ_API_KEY=your_actual_key_here<\/code><\/p>\n\n\n\n<p>Replace your_actual_key_here with the key you just copied from Groq.<\/p>\n\n\n\n<p><strong>Why are we doing this?<\/strong> Your API key is like a password. If you write it directly in your code and share the code with someone, they can use your account. Storing it in a separate .env file keeps it private and out of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 2: Writing the Code<\/strong><\/h2>\n\n\n\n<p>Create a new file called agent.py in your langchain_agent folder. Copy and paste the full code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\nfrom dotenv import load_dotenv\n\nfrom langchain_groq import ChatGroq\n\nfrom langchain.agents import create_react_agent, AgentExecutor\n\nfrom langchain.memory import ConversationBufferMemory\n\nfrom langchain.prompts import PromptTemplate\n\nfrom langchain_community.tools import WikipediaQueryRun\n\nfrom langchain_community.utilities import WikipediaAPIWrapper<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 1: Load your API key from the .env file <\/strong><\/h3>\n\n\n\n<p> This reads your secret key from the .env file so you don&#8217;t have to write it directly in the code<\/p>\n\n\n\n<p><code>load_dotenv()<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 2: Connect to the AI model <\/strong><\/h3>\n\n\n\n<p>We&#8217;re using Groq&#8217;s free Llama 3.3 model as the brain of our agent. Temperature=0 means the AI will be focused and consistent, not random or overly creative.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>llm = ChatGroq(\n\n&nbsp;&nbsp;&nbsp;&nbsp;model=\"llama-3.3-70b-versatile\",\n\n&nbsp;&nbsp;&nbsp;&nbsp;temperature=0,\n\n&nbsp;&nbsp;&nbsp;&nbsp;api_key=os.getenv(\"GROQ_API_KEY\")\n\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong> STEP 3: Give the agent a tool: Wikipedia search <\/strong><\/h3>\n\n\n\n<p>This is what lets the agent actually look things up. Top_k_results=2 means it will fetch the top 2 Wikipedia results<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wiki_tool = WikipediaQueryRun(\n\n&nbsp;&nbsp;&nbsp;&nbsp;api_wrapper=WikipediaAPIWrapper(top_k_results=2)\n\n)\n\n# Put all tools into a list\n\n# You can add more tools to this list later\n\ntools = &#91;wiki_tool]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 4: Give the agent memory <\/strong><\/h3>\n\n\n\n<p>Without this, the agent forgets everything after each message. With this, it remembers the full conversation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>memory = ConversationBufferMemory(\n\n&nbsp;&nbsp;&nbsp;&nbsp;memory_key=\"chat_history\",\n\n&nbsp;&nbsp;&nbsp;&nbsp;return_messages=True\n\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 5: Write the thinking instructions (the prompt) <\/strong><\/h3>\n\n\n\n<p>This is what teaches the agent HOW to think. It tells it to follow the Think \u2192 Act \u2192 Observe format. Do not change the words inside {}, those are placeholders that LangChain fills in automatically<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template = \"\"\"Answer the following questions as best you can.\n\nYou have access to the following tools:\n\n{tools}\n\nUse the following format strictly:\n\nQuestion: the input question you must answer\n\nThought: you should always think about what to do\n\nAction: the action to take, should be one of &#91;{tool_names}]\n\nAction Input: the input to the action\n\nObservation: the result of the action\n\n... (this Thought\/Action\/Action Input\/Observation can repeat up to 5 times)\n\nThought: I now know the final answer\n\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: {input}\n\nThought:{agent_scratchpad}\"\"\"\n\nprompt = PromptTemplate.from_template(template)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong> STEP 6: Build the agent <\/strong><\/h3>\n\n\n\n<p>This combines the brain (llm), the tools, and the instructions (prompt) into one agent object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>agent = create_react_agent(\n\n&nbsp;&nbsp;&nbsp;&nbsp;llm=llm,\n\n&nbsp;&nbsp;&nbsp;&nbsp;tools=tools,\n\n&nbsp;&nbsp;&nbsp;&nbsp;prompt=prompt\n\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 7: Wrap it in an executor <\/strong><\/h3>\n\n\n\n<p>The agent decides what to do, but the executor actually does it<\/p>\n\n\n\n<p># verbose=True \u2192 shows you every thought and action in the terminal<\/p>\n\n\n\n<p># max_iterations=5 \u2192 stops after 5 loops so it never runs forever<\/p>\n\n\n\n<p># handle_parsing_errors=True \u2192 if the AI writes something weird,<\/p>\n\n\n\n<p>#&nbsp;it tries again instead of crashing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>agent_executor = AgentExecutor(\n\n&nbsp;&nbsp;&nbsp;&nbsp;agent=agent,\n\n&nbsp;&nbsp;&nbsp;&nbsp;tools=tools,\n\n&nbsp;&nbsp;&nbsp;&nbsp;memory=memory,\n\n&nbsp;&nbsp;&nbsp;&nbsp;verbose=True,\n\n&nbsp;&nbsp;&nbsp;&nbsp;max_iterations=5,\n\n&nbsp;&nbsp;&nbsp;&nbsp;handle_parsing_errors=True\n\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>STEP 8: Ask your question <\/strong><\/h3>\n\n\n\n<p>Change the text inside the quotes to ask anything you want<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"\\n\ud83e\udd16 Agent is thinking...\\n\")\n\nresponse = agent_executor.invoke({\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"input\": \"What is LangChain and what is it used for?\"\n\n})\n\nprint(\"\\n\u2705 Final Answer:\")\n\nprint(response&#91;\"output\"])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 3: Running the Code<\/strong><\/h2>\n\n\n\n<p>Make sure your virtual environment is still active (you should see (langchain_env) in your terminal), then run:<\/p>\n\n\n\n<p><code>python agent.py<\/code><\/p>\n\n\n\n<p>You&#8217;ll see something like this in your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud83e\udd16 Agent is thinking...\n\n&gt; Entering new AgentExecutor chain...\n\nThought: I should look up LangChain on Wikipedia\n\nAction: wikipedia\n\nAction Input: LangChain\n\nObservation: LangChain is a framework for developing applications...\n\nThought: I now know the final answer\n\nFinal Answer: LangChain is an open-source framework...\n\nFinal Answer:\n\nLangChain is an open-source framework designed to help developers build applications powered by large language models...<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"371\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1200x371.png\" alt=\"\" class=\"wp-image-103235\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1200x371.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-300x93.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-768x238.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1536x475.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-150x46.png 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18.png 1600w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"371\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1200x371.png\" alt=\"\" class=\"wp-image-103236\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1200x371.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-300x93.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-768x238.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-1536x475.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18-150x46.png 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/image-18.png 1600w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>That&#8217;s your agent working. It thought, searched, read the result, and gave you an answer, all on its own.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 4: Making It Your Own<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Change the Question<\/strong><\/h3>\n\n\n\n<p>Find this line at the bottom of your agent.py:<\/p>\n\n\n\n<p>&#8220;input&#8221;: &#8220;What is LangChain and what is it used for?&#8221;<\/p>\n\n\n\n<p>Change it to anything you like:<\/p>\n\n\n\n<p>&#8220;input&#8221;: &#8220;What is artificial intelligence?&#8221;<\/p>\n\n\n\n<p>&#8220;input&#8221;: &#8220;Who is Elon Musk?&#8221;<\/p>\n\n\n\n<p>&#8220;input&#8221;: &#8220;What is the Python programming language?&#8221;<\/p>\n\n\n\n<p>&#8220;input&#8221;: &#8220;Explain machine learning in simple terms&#8221;<\/p>\n\n\n\n<p>Save the file and run python agent.py again each time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Have a Multi-Turn Conversation<\/strong><\/h3>\n\n\n\n<p>Want to ask follow-up questions? Replace the bottom section of your code with this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"\\n\ud83e\udd16 Agent is ready! Type your question below.\")\n\nprint(\"Type 'quit' to exit.\\n\")\n\nwhile True:\n\n&nbsp;&nbsp;&nbsp;&nbsp;user_input = input(\"You: \")\n\n&nbsp;&nbsp;&nbsp;&nbsp;if user_input.lower() == \"quit\":\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"Goodbye!\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n\n&nbsp;&nbsp;&nbsp;&nbsp;response = agent_executor.invoke({\"input\": user_input})\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(f\"\\n\u2705 Agent: {response&#91;'output']}\\n\")<\/code><\/pre>\n\n\n\n<p>Now, when you run the script, it keeps asking for your input until you type `quit`. And because of the memory we set up, it remembers everything from earlier in the conversation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Quick Recap: What You Just Built<\/strong><\/h2>\n\n\n\n<p>Here&#8217;s the plain English version of what every part of your code does:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Part<\/strong><\/td><td><strong>What It Does In Plain English<\/strong><\/td><\/tr><tr><td>load_dotenv()<\/td><td>Reads your secret API key from the .env file<\/td><\/tr><tr><td>ChatGroq(&#8230;)<\/td><td>Connects to the free Groq AI model<\/td><\/tr><tr><td>WikipediaQueryRun<\/td><td>Gives the agent the ability to search Wikipedia<\/td><\/tr><tr><td>ConversationBufferMemory<\/td><td>Gives the agent a memory so it remembers the conversation<\/td><\/tr><tr><td>PromptTemplate<\/td><td>Teaches the agent how to think step by step<\/td><\/tr><tr><td>create_react_agent<\/td><td>Combines the brain, tools, and instructions into one agent<\/td><\/tr><tr><td>AgentExecutor<\/td><td>Runs the think \u2192 act \u2192 observe loop and returns the answer<\/td><\/tr><tr><td>agent_executor.invoke<\/td><td>This is where you actually ask the agent a question<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Quick Recap<\/strong><\/figcaption><\/figure>\n\n\n\n<p>If you\u2019re serious about mastering LLMs and want to apply it in real-world scenarios, don\u2019t miss the chance to enroll in HCL GUVI\u2019s <strong>Intel &amp; IITM Pravartak Certified<\/strong><a href=\"https:\/\/www.guvi.in\/mlp\/artificial-intelligence-and-machine-learning\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=custom-langchain-agent-for-llm-applications\" target=\"_blank\" rel=\"noreferrer noopener\"><strong> Artificial Intelligence &amp; Machine Learning course<\/strong><\/a>. Endorsed with <strong>Intel certification<\/strong>, this course adds a globally recognized credential to your resume, a powerful edge that sets you apart in the competitive AI job market.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, building a LangChain agent from scratch might have felt intimidating at the start, but as you&#8217;ve seen, it really comes down to a few moving parts: a model, some tools, a memory, and a prompt, all wired together.&nbsp;<\/p>\n\n\n\n<p>You&#8217;ve gone from zero to a fully working AI agent that can think, search, and hold a conversation, entirely for free. The errors you hit along the way weren&#8217;t setbacks; they were actually the most valuable part of the learning process, because now you know exactly how to handle them when they show up again.&nbsp;<\/p>\n\n\n\n<p>From here, the possibilities are wide open, you can add more tools, connect the agent to your own data, or scale it into a full application. The hard part is already behind you. What you build next is entirely up to you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1772884190019\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is LangChain used for?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>LangChain is used to build applications powered by large language models. It lets you connect AI models to external tools like search engines, databases, and APIs so they can do more than just answer from memory.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1772884192003\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Is LangChain free to use?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, LangChain itself is completely free and open source. However, the AI model you connect it to may have costs. OpenAI charges per API call, while alternatives like Groq offer free access to powerful models.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1772884196002\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. What is the difference between a LangChain chain and a LangChain agent?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A chain follows a fixed, predefined sequence of steps every single time; it doesn&#8217;t make decisions. An agent, on the other hand, decides dynamically which steps to take based on the question asked.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1772884204818\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. Do I need to know machine learning to use LangChain?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, you don&#8217;t need any machine learning background to get started with LangChain. As long as you have basic Python knowledge, you can build agents, chatbots, and pipelines using LangChain&#8217;s ready-made components.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1772884211618\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. What is the best alternative to LangChain?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The most popular alternatives to LangChain are LlamaIndex, Haystack, and AutoGen. LlamaIndex is great for document retrieval and RAG applications, Haystack is preferred for enterprise search pipelines, and AutoGen is better suited for multi-agent systems where multiple AI agents collaborate.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Imagine you have a really smart assistant. It knows a lot of things, but it can&#8217;t look anything up on its own. It just answers from memory. That&#8217;s basically what a regular AI model does: it responds based on what it was trained on, but it can&#8217;t go out and fetch new information. A LangChain [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":103672,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[933,715],"tags":[],"views":"471","authorinfo":{"name":"Lukesh S","url":"https:\/\/www.guvi.in\/blog\/author\/lukesh\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/LangChain-Agent-for-LLM-Applications-in-Python-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/03\/LangChain-Agent-for-LLM-Applications-in-Python.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/103234"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=103234"}],"version-history":[{"count":7,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/103234\/revisions"}],"predecessor-version":[{"id":103675,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/103234\/revisions\/103675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/103672"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=103234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=103234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=103234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}