{"slug": "running-nvidia-nemotron-on-langchain-via-openrouter", "title": "Running Nvidia Nemotron on LangChain via OpenRouter", "summary": "Step-by-step guide on how to build a LangChain agent that utilizes Nvidia's free Nemotron AI models via the OpenRouter platform. It covers the entire setup process, from installing necessary packages and configuring API keys to creating a simple tool-calling agent that can execute functions like retrieving weather data. The guide also offers tips for expanding the agent with multiple tools and suggests other free models for further experimentation.", "body_md": "Nvidia's Nemotron models are powerful, free-to-use AI models available through OpenRouter. In this guide, you'll learn how to set up a LangChain agent that uses Nemotron — from installing packages to running your first tool-calling agent.\n\n## What Are We Building?\n\nA simple LangChain agent that:\n\n- Uses\n**Nvidia Nemotron** as its brain (via OpenRouter's free tier) - Has access to a custom\n**tool**(a weather function) - Answers questions by calling that tool automatically\n\n## Prerequisites\n\n- Python 3.10 or higher\n- A free\n[OpenRouter account](https://openrouter.ai/)and API key -\n[uv](https://docs.astral.sh/uv/)(recommended) or pip\n\n## Step 1 — Get Your OpenRouter API Key\n\n- Go to\n[openrouter.ai/settings/keys](https://openrouter.ai/settings/keys) - Click\n**Create Key** - Copy the key — you'll need it shortly\n\nOpenRouter gives you free access to many models, including Nvidia's Nemotron family, with no credit card required.\n\n## Step 2 — Set Up Your Project\n\nCreate a new project folder and initialize it:\n\n```\nmkdir my-nemotron-agent\ncd my-nemotron-agent\nuv init\n```\n\nOr if you're using pip, just create a folder and a virtual environment:\n\n```\nmkdir my-nemotron-agent\ncd my-nemotron-agent\npython -m venv .venv\n.venv\\Scripts\\activate   # Windows\n# source .venv/bin/activate  # Mac/Linux\n```\n\n## Step 3 — Install Dependencies\n\n```\nuv add langchain langchain-openrouter python-dotenv\n```\n\nOr with pip:\n\n```\npip install langchain langchain-openrouter python-dotenv\n```\n\nThese three packages are all you need:\n\n-\n**langchain**— the agent framework -\n**langchain-openrouter**— connects LangChain to OpenRouter's API -\n**python-dotenv**— loads your API key from a`.env`\n\nfile safely\n\n## Step 4 — Store Your API Key\n\nCreate a `.env`\n\nfile in your project folder (never commit this to Git!):\n\n```\nOPENROUTER_API_KEY=your-key-here\n```\n\nAlso create a `.gitignore`\n\nto protect it:\n\n```\n.env\n.venv\n```\n\n## Step 5 — Choose a Nemotron Model\n\nOpenRouter hosts several free Nvidia Nemotron models. Here are the main ones:\n\n| Model | ID | Best For |\n|---|---|---|\n| Nemotron 3 Nano 30B | `nvidia/nemotron-3-nano-30b-a3b:free` |\nFast, general tasks |\n| Nemotron 3 Super 120B | `nvidia/nemotron-3-super-120b-a12b:free` |\nComplex reasoning |\n| Nemotron Nano 9B V2 | `nvidia/nemotron-nano-9b-v2:free` |\nLightweight tasks |\n\nNote:The`:free`\n\nsuffix is required. Without it, OpenRouter will look for a paid endpoint.\n\nFor beginners, `nvidia/nemotron-3-nano-30b-a3b:free`\n\nis a great starting point — it's fast and capable.\n\n## Step 6 — Write Your Agent\n\nCreate a file called `main.py`\n\n:\n\n``` python\nfrom dotenv import load_dotenv\nfrom langchain.agents import create_agent\n\n# Load the OPENROUTER_API_KEY from your .env file\nload_dotenv()\n\n# Define a tool — any plain Python function works!\ndef get_weather(city: str) -> str:\n    \"\"\"Get weather for a given city.\"\"\"\n    return f\"It's always sunny in {city}!\"\n\n# Create the agent\nagent = create_agent(\n    model=\"openrouter:nvidia/nemotron-3-nano-30b-a3b:free\",\n    tools=[get_weather],\n    system_prompt=\"You are a helpful assistant\",\n)\n\n# Run the agent\nresult = agent.invoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"What's the weather in San Francisco?\"}]}\n)\n\n# Print the final response\nprint(result[\"messages\"][-1].content)\n```\n\n### How It Works\n\n-\n`load_dotenv()`\n\nreads your`.env`\n\nfile and sets the`OPENROUTER_API_KEY`\n\nenvironment variable -\n`get_weather`\n\nis a regular Python function — LangChain uses its**docstring** to describe the tool to the model -\n`create_agent`\n\nwires everything together: the model, the tools, and the system prompt -\n`agent.invoke(...)`\n\nsends the user's message and returns a result dict -\n`result[\"messages\"][-1].content`\n\ngets the final text response from the last message\n\n## Step 7 — Run It\n\n```\nuv run main.py\n```\n\nOr with pip/venv:\n\n```\npython main.py\n```\n\nYou should see something like:\n\n```\nThe weather in San Francisco is sunny!\n```\n\n## Step 8 — Add More Tools\n\nYou can give your agent multiple tools — just add more functions to the list:\n\n``` php\ndef get_weather(city: str) -> str:\n    \"\"\"Get the current weather for a city.\"\"\"\n    return f\"It's always sunny in {city}!\"\n\ndef get_population(city: str) -> str:\n    \"\"\"Get the population of a city.\"\"\"\n    populations = {\n        \"San Francisco\": \"870,000\",\n        \"New York\": \"8,300,000\",\n        \"Dhaka\": \"21,000,000\",\n    }\n    return populations.get(city, \"Population data not available.\")\n\nagent = create_agent(\n    model=\"openrouter:nvidia/nemotron-3-nano-30b-a3b:free\",\n    tools=[get_weather, get_population],\n    system_prompt=\"You are a helpful assistant\",\n)\n```\n\nThe agent will automatically decide which tool to call based on the user's question.\n\n## Common Errors & Fixes\n\n| Error | Cause | Fix |\n|---|---|---|\n`OPENROUTER_API_KEY must be set` |\nMissing API key | Add it to your `.env` file and call `load_dotenv()`\n|\n`is not a valid model ID` |\nWrong model name | Use the exact ID from openrouter.ai/models, with `:free` suffix |\n`No endpoints found` |\nModel unavailable | Try a different Nemotron model ID |\n`cannot import name 'create_agent'` |\nWrong Python / old langchain | Use `uv run` instead of `python` , or upgrade langchain |\n\n## Project Structure\n\nWhen you're done, your project should look like this:\n\n```\nmy-nemotron-agent/\n├── .env              ← Your API key (never share this!)\n├── .gitignore        ← Excludes .env from Git\n├── main.py           ← Your agent code\n└── pyproject.toml    ← Dependencies (if using uv)\n```\n\n## Next Steps\n\nOnce you're comfortable with the basics, here's what to explore next:\n\n-\n**Real tools**— connect to real APIs (weather, search, databases) instead of mock functions -\n**Memory**— give your agent conversation history so it remembers past messages -\n**Streaming**— stream the response token by token for a ChatGPT-like feel -\n**LangSmith**— trace and debug your agent's reasoning visually -\n**Other free models**— try`deepseek/deepseek-r1:free`\n\nor`google/gemma-3-27b-it:free`\n\nfor comparison\n\nHappy building!", "url": "https://wpnews.pro/news/running-nvidia-nemotron-on-langchain-via-openrouter", "canonical_source": "https://dev.to/syeedmdtalha/running-nvidia-nemotron-on-langchain-via-openrouter-21j0", "published_at": "2026-05-20 09:46:09+00:00", "updated_at": "2026-05-20 10:05:02.790302+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools", "open-source"], "entities": ["Nvidia", "Nemotron", "LangChain", "OpenRouter"], "alternates": {"html": "https://wpnews.pro/news/running-nvidia-nemotron-on-langchain-via-openrouter", "markdown": "https://wpnews.pro/news/running-nvidia-nemotron-on-langchain-via-openrouter.md", "text": "https://wpnews.pro/news/running-nvidia-nemotron-on-langchain-via-openrouter.txt", "jsonld": "https://wpnews.pro/news/running-nvidia-nemotron-on-langchain-via-openrouter.jsonld"}}