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 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.

// 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
  }
}];
```text
Then branch with an IF node:

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

True branch:  [OpenAI: Generate Description] --> [Save to DB with new hash]
False branch: [Return Cached Description] --> [Done]
```text
```yaml
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%
```text
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](../ai-and-llm-integration/01-use-structured-output-json-mode-for-parseable-responses.md) | [Configure Payload Size and Binary Data Mode for Large Files](../performance-and-large-files/01-configure-payload-size-and-binary-data-mode-for-large-files.md)

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

191 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 a 20-minute call