Sparround

Basic LLM Chain and prompts

The Basic LLM Chain is the entry point to AI in n8n: one root node with a Chat Model sub-node attached. Input arrives, the model is called, an answer comes out.

The four tasks that come up most in automation are solved by exactly this node:

  • Classification — tickets, emails, reviews
  • Field extraction — structured data from free text
  • Summarisation — shortening long text (the Summarization Chain also exists for this)
  • Rewriting — changing tone, translating, reformatting

When you need question-answering over documents, use the Question and Answer Chain — it connects to a vector store through a retriever, or to a workflow.

Writing prompts for automation is different. In a chat window you read the model's answer and correct it. In a workflow the answer goes straight to the next node — there is no human in between. That places three demands on the prompt:

1. The output format must be specified exactly. "Return JSON" is not enough; which fields, which types — all of it. 2. Behaviour for the unknown case must be defined. What should the model do when it cannot find the information? Leave it blank? Return null? Not invent it — that has to be written down. 3. The options must be constrained. For classification, give the list of permitted values, or the model will coin a new label each time.

n8n also has a Structured Output Parser sub-node, which helps enforce that the output matches an expected schema. That is more reliable than describing the format in the prompt.

ProblemCauseFix
The answer is sometimes JSON, sometimes proseThe format is not specified precisely enoughAttach a Structured Output Parser; show an example in the prompt
The model writes a different category name each timeNo list of permitted values was givenList them explicitly and say "return only one of these"
The model invents information it could not findNo behaviour was defined for the unknown caseGive an explicit instruction: "return `null` if you cannot find it"
The result varies for the same inputThe model's randomness setting is highLower parameters such as temperature on the Chat Model sub-node
The answer is cut short on long inputsThe input or output limit was reachedSplit the text, process in parts and combine the results

AI output must always be validated. A model can produce a convincing but wrong result. If the next node writes to a database or messages a customer, an IF check belongs in between: are the required fields present, is the number really a number, is the category in the permitted list?

📚 Sources and documentation