{
  "source_file": "0930",
  "source_platform": "Ghost",
  "author": "Deep Learning Nerds",
  "title_listed": "LangChain Tools Explained for Beginners: Add Function Calling to Your AI Agent",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://www.deeplearningnerds.com/langchain-tools-function-calling-ai-agent/",
  "publish_date": "2026-06",
  "corpus_stage": "",
  "notes": "",
  "id": "fc925dcad4e7",
  "collected_at": "2026-10-02T12:24:59",
  "title": "LangChain Tools Explained for Beginners: Add Function Calling to Your AI Agent",
  "body_markdown": "📘 Introduction\nA basic LLM can generate text, but an AI agent often needs to do more than write an answer. It may need to calculate a price, look up product information, check a database, call an API, or run a small business rule. LangChain tools make this possible by exposing Python functions to a chat model in a structured way.\nIn this tutorial, you will learn what LangChain tools are, why tool calling matters for AI agents, and how to define a simple custom tool in Python. Then we will connect that tool to a chat model so the model can decide when to call it.\n🛠️\nTools are what let an AI agent take action. Instead of only producing text, the model can request a function call with structured arguments.\n💡 What are LangChain tools?\nA LangChain tool is a callable function with a name, description, and input schema. The description helps the model understand when the tool should be used. The input schema helps the model provide the right arguments.\nYou can think of a normal Python function as something your code calls directly. A LangChain tool is a Python function prepared so a model can request it during a conversation.\nConcept\nWhat it means\nBeginner example\nPython function\nCode your program can call directly\ncalculate_discount(100, 20)\nLangChain tool\nA function described for model tool calling\nA discount calculator tool\nTool calling\nThe model asks to use a tool with arguments\nCall the calculator with price and discount\n🧠 Why tools matter for AI agents\nTools make AI agents more useful because they connect language understanding with real operations. A model can understand the user request, choose the right tool, provide structured inputs, and use the tool result to continue the conversation.\nUser request -> Model decides tool call -> Python tool runs -> Tool result returns -> Model answers\n✅\nTools are powerful, so keep them narrow and safe. A beginner tool should do one clear task with predictable inputs and outputs.\n✅ Prerequisites\nBefore getting started, make sure you have:\n☑️ Python installed\n☑️ Basic Python knowledge\n☑️ An OpenAI API key or another tool-calling chat model provider\n☑️ A terminal or command prompt\n⚙️1️⃣ Create a project folder\nCreate a new local project folder for this tutorial:\nmkdir langchain-tools-agent\ncd langchain-tools-agent\n🐍2️⃣ Create a virtual environment\nCreate and activate a virtual environment:\npython -m venv .venv\nsource .venv/bin/activate\nOn Windows, activate it with:\n.venv\\Scripts\\activate\n📦3️⃣ Install libraries\nInstall the packages we need for tools and the chat model integration:\npip install -U langchain langchain-openai\n🔐4️⃣ Set your API key\nSet your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.\nexport OPENAI_API_KEY=\"your_api_key_here\"\nOn Windows PowerShell, use:\n$env:OPENAI_API_KEY=\"your_api_key_here\"\n🔐\nUse placeholders in tutorials and environment variables in real projects. Do not write private API keys directly into Python files.\n🛠️5️⃣ Define your first LangChain tool\nCreate a file named\ndefine_tool.py\n. In this public example, we define a small discount calculator tool and call it directly so you can understand the tool object before adding a model.\nfrom langchain.tools import tool\n\n\n@tool\ndef calculate_discount(price: float, discount_percent: float) -> str:\n    \"\"\"Calculate the final price after a percentage discount.\"\"\"\n    if price < 0:\n        return \"Price must be greater than or equal to 0.\"\n    if discount_percent < 0 or discount_percent > 100:\n        return \"Discount percent must be between 0 and 100.\"\n\n    final_price = price * (1 - discount_percent / 100)\n    return f\"Final price: ${final_price:.2f}\"\n\n\nprint(calculate_discount.name)\nprint(calculate_discount.description)\nprint(calculate_discount.invoke({\"price\": 120, \"discount_percent\": 25}))\ndefine_tool.py\nRun the script:\npython define_tool.py\nYou should see the tool name, description, and the calculated result:\ncalculate_discount\nCalculate the final price after a percentage discount.\nFinal price: $90.00\n🎓\nWant the model-connected workflow? In the Academy section, we bind this tool to a chat model and inspect the tool call the model requests.\nYou can view this post with the tier:\n      Academy Membership\nJoin academy now to read the post and get access to the full library of premium posts for academy members only.\nJoin Academy\nAlready have an account?\nSign In",
  "metadata": {
    "title": "LangChain Tools Explained for Beginners: Add Function Calling to Your AI Agent",
    "description": "Learn how LangChain tools let AI agents call Python functions, use structured inputs, and go beyond plain text generation.",
    "keywords": null,
    "author": null,
    "og:site_name": "Deep Learning Nerds | The ultimate Learning Platform for AI and Data Science",
    "og:type": "article",
    "og:title": "LangChain Tools Explained for Beginners: Add Function Calling to Your AI Agent",
    "og:description": "Learn how LangChain tools let AI agents call Python functions, use structured inputs, and go beyond plain text generation.",
    "og:url": "https://www.deeplearningnerds.com/langchain-tools-function-calling-ai-agent/",
    "og:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/LangChain-Tools-Explained-for-Beginners.png",
    "og:image:width": "1200",
    "og:image:height": "839",
    "twitter:card": "summary_large_image",
    "twitter:title": "LangChain Tools Explained for Beginners: Add Function Calling to Your AI Agent",
    "twitter:description": "Learn how LangChain tools let AI agents call Python functions, use structured inputs, and go beyond plain text generation.",
    "twitter:url": "https://www.deeplearningnerds.com/langchain-tools-function-calling-ai-agent/",
    "twitter:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/LangChain-Tools-Explained-for-Beginners.png",
    "twitter:label1": "Written by",
    "twitter:data1": "AI Developer",
    "twitter:label2": "Filed under",
    "twitter:data2": "LangChain, Python, Generative AI, Artificial Intelligence, Agentic AI",
    "twitter:site": "@DLNerds",
    "article:published_time": "2026-06-04T17:52:54.000Z",
    "article:modified_time": "2026-06-04T17:52:54.000Z",
    "article:tag": "Agentic AI",
    "article:publisher": "https://www.facebook.com/deeplearningnerds/",
    "article:author": "https://www.facebook.com/deeplearningnerds/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/fc925dcad4e7/code/body-000.txt",
      "chars": 99,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-001.txt",
      "chars": 52,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-002.txt",
      "chars": 46,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-003.txt",
      "chars": 22,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-004.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-005.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-006.txt",
      "chars": 39,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-007.txt",
      "chars": 614,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-008.txt",
      "chars": 21,
      "language": "text"
    },
    {
      "file": "attachments/fc925dcad4e7/code/body-009.txt",
      "chars": 93,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://www.deeplearningnerds.com/membership/",
      "label": "Join Academy",
      "type": "a"
    },
    {
      "href": "https://www.deeplearningnerds.com/signin/",
      "label": "Sign In",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:47:42Z",
  "source_url": "https://www.deeplearningnerds.com/langchain-tools-function-calling-ai-agent/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 4427,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/fc925dcad4e7/pages/source.html",
  "local_html": "attachments/fc925dcad4e7/pages/article.html",
  "body_base_path": "attachments/fc925dcad4e7/pages/",
  "body_markdown_path": "attachments/fc925dcad4e7/pages/article.md"
}