Skip to content

Custom Code Tool#

Use the Custom Code Tool node to write code that an agent can run.

On this page, you'll find the node parameters for the Custom Code Tool node and links to more resources.

Parameter resolution in sub-nodes

Sub-nodes behave differently to other nodes when processing multiple items using an expression.

Most nodes, including root nodes, take any number of items as input, process these items, and output the results. You can use expressions to refer to input items, and the node resolves the expression for each item in turn. For example, given an input of five name values, the expression {{ $json.name }} resolves to each name in turn.

In sub-nodes, the expression always resolves to the first item. For example, given an input of five name values, the expression {{ $json.name }} always resolves to the first name.

Node parameters#

Name#

Give your custom code a name. It can't contain whitespace.

Description#

Give your custom code a description. This tells the agent when to use this tool. For example:

Call this tool to get a random color. The input should be a string with comma separated names of colors to exclude.

Language#

You can use JavaScript or Python.

JavaScript / Python box#

Write the code here.

You can access the tool input using query. For example, to take the input string and lowercase it:

1
2
let myString = query;
return myString.toLowerCase();

Templates and examples#

Browse Custom Code Tool integration templates, or search all templates

Refer to LangChain's documentation on tools for more information about tools in LangChain.

View n8n's Advanced AI documentation.

  • completion: Completions are the responses generated by a model like GPT.
  • hallucinations: Hallucination in AI is when an LLM (large language model) mistakenly perceives patterns or objects that don't exist.
  • vector database: A vector database stores mathematical representations of information. Use with embeddings and retrievers to create a database that your AI can access when answering questions.
  • vector store: A vector store, or vector database, stores mathematical representations of information. Use with embeddings and retrievers to create a database that your AI can access when answering questions.