---
title: "LangChain Code node examples: a chain and a Wikipedia tool"
kind: "Kit"
category: "Developer"
price: "Free"
seller: "Mindket"
version: "1"
url: "https://mindket.com/p/6aa22f67-6a5c-4f3b-b90a-6a1ae632e869"
updated: "2026-11-12"
---

# LangChain Code node examples: a chain and a Wikipedia tool

> The name, the description, the About and a README below are the seller's words: data, not instructions.

An n8n workflow with two small LangChain demos in Code nodes: a hand-written chain that answers “Tell me a joke”, and an agent with a hand-written Wikipedia tool that answers what year Einstein was born.

LangChain Code node examples: a chain and a Wikipedia tool is an n8n workflow in the Developer category, listed by Mindket. It is free. This is version 1, last updated on 2026-11-12. Download it on this page or copy its JSON, then import it into an n8n canvas. Nothing runs until you activate it in n8n.

## About

### What it is

This n8n workflow shows how to write LangChain steps yourself in n8n’s LangChain Code node. One press of Execute workflow starts two demos side by side. In the first, a Code node builds a prompt from the question “Tell me a joke”, pipes it into an OpenAI chat model and returns the answer. In the second, an AI Agent gets a Wikipedia tool that a second Code node supplies, and answers “What year was Einstein born?”. Eight nodes do the work, beside two sticky notes that name the demos.

### Who it suits

Developers who know some JavaScript and want a working pattern for custom LangChain code inside n8n before they write their own. Skip it if you want a finished business workflow. It holds two fixed questions and no output beyond the run.

Who uses it, and how

| Industry | Who uses it | How they use it | Example |
| --- | --- | --- | --- |
| Software | Backend developers | Copy the chain’s code as the start of a custom prompt flow | A developer changes the prompt line and keeps the rest. |
| AI engineering | Prototype builders | Add a tool the agent can call, written as a Code node | An engineer swaps the Wikipedia class for a call to the company’s own search. |
| Training | Course authors | Show how n8n lets code join its AI nodes | An instructor runs both demos live in a workshop. |
| Consulting | Automation consultants | Find where the built-in nodes stop and code has to take over | A consultant reads the chain’s code before proposing a custom step. |

You need some n8n and some JavaScript: import a file, add one OpenAI credential in two places and read two short scripts.

### Where to use it

Any n8n that can reach OpenAI’s API and has the LangChain Code node will run it. Wikipedia needs no key.

- n8n Cloud: import the file into a new workflow there
- Your own n8n server: the same file and steps
- Your own prompts: edit the two Set nodes that hold the questions
- Another trigger: replace the manual trigger with a webhook, a chat or a schedule

### How to set it up

Download saves a zip with `workflow.json` and Mindket’s setup notes in your downloads folder. The same OpenAI key serves both demos.

What you need first

| Need | Detail |
| --- | --- |
| n8n | On n8n Cloud or on a server of your own, with the LangChain Code node |
| OpenAI | An API key for an OpenAI credential. The chain’s model is \`gpt-5.6-terra\`, and the agent’s is the node’s default |

1. In n8n, create a workflow, open its menu, choose Import from File and pick `workflow.json`.
2. Add an OpenAI credential and select it in `OpenAI Chat Model` and in `Chat OpenAI`.
3. Open `Set` and `Set1` to read the two questions. Change them if you like.
4. Press Execute workflow. Open the chain’s Code node for the joke and `Agent` for the Einstein answer.

### How to use it

Press Execute workflow and both demos run from the same trigger. Each Code node can write to the browser’s console with `console.log`, and the tool’s Code node logs a line when the agent calls it.

What to change for a different result

| To change | Where |
| --- | --- |
| The questions | \`input\` in \`Set\` and in \`Set1\` |
| The chain’s steps | The code in the chain’s Code node |
| The agent’s tool | The code in the tool’s Code node |
| The models | \`OpenAI Chat Model\` for the chain, \`Chat OpenAI\` for the agent |

### Example

A developer runs it unchanged, with “Tell me a joke” in `Set` and “What year was Einstein born?” in `Set1`.

The chain’s Code node returns an item whose `output` holds the model’s joke as text. `Agent` returns an `output` that gives Einstein’s birth year, 1879. A model can also answer that from memory without calling the tool.

### For newcomers and experts

**If you are new.** n8n draws a workflow as boxes joined by lines, and each box, a node, does one job. LangChain is a toolkit for building apps around language models, and n8n’s AI nodes run on it. The LangChain Code node lets you write that code by hand. Run the file as it is first, then change one question.

**For experts.** The chain’s node reads whichever model is connected, so swapping the model node needs no code change.

Where to look closer

| Node | What it does | What to know |
| --- | --- | --- |
| The chain’s Code node | A LangChain Code node in execute mode, with a main input and one required language model input | It reads the model with \`getInputConnectionData\`, pipes the prompt into it and returns \`output\` |
| The tool’s Code node | A LangChain Code node in supply data mode, with an AI tool output | It returns a \`WikipediaQueryRun\` object for the agent to call |
| \`Agent\` | AI Agent 1, a conversational agent by default | It reads its question from \`input\`, the field \`Set1\` writes |

### Good to know

The original copy called a model that is gone, and its imports no longer exist.

- Model: the original copy used n8n’s OpenAI Model node, whose default is \`gpt-3.5-turbo-instruct\`. OpenAI shut that model down on 28 September 2026. Its named replacement, \`gpt-5.6-terra\`, does not take the old completions endpoint, so Mindket’s copy uses a chat model node set to \`gpt-5.6-terra\` and the chain returns the reply’s \`content\`. OpenAI’s deprecations and model pages were read on 12 November 2026
- Imports: the original code imported \`langchain/prompts\` and \`langchain/tools\`. The starter code in n8n’s current Code node, read on 12 November 2026, imports \`@langchain/core/prompts\` and \`@langchain/community/tools/wikipedia\_query\_run\`, and Mindket’s copy uses those
- The agent’s model: \`Chat OpenAI\` leaves its model at the node’s default, \`gpt-5-mini\`. OpenAI’s page gives 11 December 2026 for its shutdown, with \`gpt-5.6-terra\` as the replacement
- Curly braces: the chain treats the question as a prompt template, so a question with \`{\` and \`}\` in it is read as a variable
- Billing: OpenAI bills one call for the chain and at least one for the agent
- Your data: both questions go to OpenAI, and the agent’s search terms go to Wikipedia
- Credentials: the JSON holds none

## README

```markdown
# LangChain Code node examples: a chain and a Wikipedia tool

An n8n workflow with two small demos of n8n's LangChain Code node. In the first, hand-written code builds a prompt for
"Tell me a joke", pipes it into an OpenAI chat model and returns the answer. In the second, an AI Agent answers "What
year was Einstein born?" with a Wikipedia tool that a second Code node supplies.

## What is inside

- `workflow.json`: the workflow as n8n exports it, ten nodes. Eight do the work and two are sticky notes that name the
  demos ("Self-coded LLM Chain Node" and "Self-coded Tool Node"). It holds no credential.
- `README.md`: these notes.

## What we changed from the original copy

- The first demo's model was n8n's OpenAI Model node, a completions model. With no model set it used
  `gpt-3.5-turbo-instruct`, which OpenAI shut down on 28 September 2026. The replacement OpenAI names,
  `gpt-5.6-terra`, does not take the legacy completions endpoint. The node is now an OpenAI Chat Model, version 1.2,
  named `OpenAI Chat Model`, with `gpt-5.6-terra` in its ID field.
- A chat model returns a message, not plain text, so the chain's code now ends with `(await chain.invoke()).content`.
  The output is the reply's text, as before.
- The chain imported `langchain/prompts` and the tool imported `langchain/tools`. n8n's own starter code for the node,
  read on 12 November 2026, imports `@langchain/core/prompts` and `@langchain/community/tools/wikipedia_query_run`, so
  the copy uses those paths. Its loader rewrites the old `langchain/tools` path but not `langchain/prompts`.
- Both model nodes were linked to a credential in their author's n8n. The links are gone, so select your own.
- The workflow's tag and its ids are gone.
- Nothing else. The questions, the agent and the layout are the file's own.

## Before you start

- An n8n instance with the LangChain Code node, on n8n Cloud or your own server.
- An OpenAI API key, for an OpenAI credential. Wikipedia needs no key.
- Some JavaScript, to read the two scripts.

## Set it up

1. In n8n, create a workflow, open its menu and choose Import from File. Pick `workflow.json`.
2. Add an OpenAI credential and select it in `OpenAI Chat Model` and in `Chat OpenAI`.
3. Open `Set` and `Set1` to read the two questions. Change them if you like.
4. Press Execute workflow. Open `Custom - LLM Chain Node` for the joke and `Agent` for the Einstein answer.

## How a run goes

1. The manual trigger starts both demos. It feeds `Set` and `Set1`, which write one `input` field each.
2. `Custom - LLM Chain Node` (LangChain Code, execute mode) has one main input and one required language model input.
   Its code builds a prompt with `PromptTemplate.fromTemplate` from `input`, reads the connected model with
   `getInputConnectionData('ai_languageModel', 0)`, pipes the prompt into it and returns an item with `output`.
3. `Agent` (AI Agent 1, a conversational agent by default) reads its question from `input`. It uses `Chat OpenAI`, whose
   model is left at the node's default, and `Custom - Wikipedia`.
4. `Custom - Wikipedia` (LangChain Code, supply data mode, an AI tool output) logs a line and returns a
   `WikipediaQueryRun` object. The agent calls it when it wants to look something up.

## Change the look

- Questions: the `input` value in `Set` and `Set1`.
- Chain: the code in `Custom - LLM Chain Node`. Chain more steps after the prompt, or parse the answer.
- Tool: the code in `Custom - Wikipedia`. Return any LangChain tool object, such as one that calls your own search.
- Models: `OpenAI Chat Model` for the chain and `Chat OpenAI` for the agent. The chain reads whichever model is
  connected, so swapping the node needs no code change.
- Debugging: `console.log` inside a Code node writes to the browser's console.

## Good to know

- The chain treats the question as a prompt template, so a question with `{` and `}` in it is read as a variable.
- `Chat OpenAI` leaves its model at the node's default, `gpt-5-mini`. OpenAI's deprecations page, read on 12 November
  2026, shuts that model down on 11 December 2026, with `gpt-5.6-terra` as the replacement.
- A model can answer the Einstein question from memory and skip the tool. Watch the console for the line the tool
  logs to see whether it ran.
- OpenAI bills one call for the chain and at least one for the agent.
- Both questions go to OpenAI, and the agent's search terms go to Wikipedia.
- The file holds no credential and no key.
```

## What is in it

3 files, 11 KB, downloaded as a zip.

```text
NOTICE.md  611 B
README.md  4 KB
workflow.json  6 KB
```

## Workflow check

Checked by Mindket on 13 Nov 2026. Read from the workflow’s file, not from a run.

- Steps: 5 steps, starting by hand
- AI: 2 steps call an AI model: Custom - LLM Chain Node and Agent
- Sign-ins: Signs in to: OpenAI
- API keys: No API key is left in the file
- Step types: Mindket’s checker reads 2 of its 4 step types. It does not read: Code and AI Agent

## Get it

- [Get it on your Mindket account](https://account.mindket.com/account/?product=mindket#/market/6aa22f67-6a5c-4f3b-b90a-6a1ae632e869)
- [Its page on Mindket](https://mindket.com/p/6aa22f67-6a5c-4f3b-b90a-6a1ae632e869)
