Tips > Data, APIs & Webhooks

Use Conditional Execution to Skip Expensive Calls When Data Has Not Changed

Before calling an expensive API (LLM, image processing, translation), check if the input data has actually changed since the last run.

Before calling an expensive API (LLM, image processing, translation), check whether the input actually changed since the last run. Store a hash of the relevant input fields alongside the previous result; if the hash matches, return the cached result and skip the call. This avoids regenerating output when only irrelevant fields changed.

Why skip expensive calls when data has not changed?

Before calling an expensive API (LLM, image processing, translation), check if the input data has actually changed since the last run. Store a hash of the input alongside the previous result. If the hash matches, return the cached result without calling the API.

Real-world example: A workflow generates AI product descriptions whenever product data is updated. But the CRM sends webhook events for every field change -- including irrelevant fields like last_viewed_at. Only regenerate descriptions when the relevant fields change.

How do you detect whether the data changed?

// Code node: "Check If Description Needs Regeneration"
const crypto = require('crypto');

const product = $input.first().json;

// Only hash the fields that affect the description
const relevantData = {
  name: product.name,
  category: product.category,
  features: product.features,
  price: product.price
};

const currentHash = crypto
  .createHash('md5')
  .update(JSON.stringify(relevantData))
  .digest('hex');

// previousHash comes from a DB/Sheet lookup node upstream
const previousHash = $input.first().json._previous_hash || null;

return [{
  json: {
    ...product,
    currentHash,
    needsRegeneration: currentHash !== previousHash
  }
}];

Then branch with an IF node:

IF node condition:
  {{ $json.needsRegeneration }} equals true

True branch:  [OpenAI: Generate Description] --> [Save to DB with new hash]
False branch: [Return Cached Description] --> [Done]

How much does this save?

Cost impact:
  Product catalog: 1000 products
  CRM events/day: ~5000 (most are irrelevant field changes)
  Actual description-relevant changes: ~50/day

  Without check: 5000 OpenAI calls/day x $0.03 = $150/day
  With check:    50 OpenAI calls/day   x $0.03 = $1.50/day
  Savings: 99%

This is especially impactful for LLM calls, where each invocation costs 10-100x more than a simple database lookup.

Related: Use Structured Output (JSON Mode) for Parseable Responses · Configure Payload Size and Binary Data Mode for Large Files

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

190 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book an introductory call