Tips > Data, APIs & Webhooks

Implement Token-Aware Prompts to Avoid Wasted Context

LLM pricing is based on tokens (input + output).

LLM pricing is based on tokens (input + output). Sending a 10,000-token document to GPT-4o when you only need information from the first paragraph wastes money. Pre-process your inputs to reduce token count before sending them to the LLM.

Real-world example: A workflow summarizes customer call transcripts. Some transcripts are 30 pages long, but the relevant content is usually in the first 5 pages and the last 2 pages (intro and conclusion). Trim the middle to save tokens.

// Code node: "Optimize LLM Input"
const transcript = $input.first().json.transcript;
const maxChars = 8000; // Roughly 2000 tokens for English text

// Strategy 1: Simple truncation with notice
if (transcript.length > maxChars) {
  const truncated = transcript.substring(0, maxChars);
  return [{
    json: {
      optimizedText: truncated + '\n\n[Transcript truncated for length]',
      originalLength: transcript.length,
      optimizedLength: truncated.length,
      estimatedTokensSaved: Math.floor((transcript.length - truncated.length) / 4)
    }
  }];
}

return [{
  json: {
    optimizedText: transcript,
    originalLength: transcript.length,
    optimizedLength: transcript.length,
    estimatedTokensSaved: 0
  }
}];
```text
A smarter approach -- keep the beginning and end, summarize the middle:

```javascript
// Code node: "Smart Transcript Trimming"
const transcript = $input.first().json.transcript;
const maxChars = 12000;

if (transcript.length <= maxChars) {
  return [{ json: { optimizedText: transcript, strategy: 'none' } }];
}

// Keep first 4000 chars (intro, context)
const head = transcript.substring(0, 4000);

// Keep last 3000 chars (conclusion, action items)
const tail = transcript.substring(transcript.length - 3000);

// Middle section -- will be sent to a cheap model for compression
const middle = transcript.substring(4000, transcript.length - 3000);

return [{
  json: {
    head,
    middle,
    tail,
    strategy: 'head-tail-with-middle-summary',
    estimatedTokensSaved: Math.floor(middle.length / 4)
  }
}];
```text
Then use a two-stage LLM pipeline:

```yaml
Stage 1 (cheap model - gpt-4o-mini):
  Input: The middle section
  Prompt: "Summarize the key points from this section in 3-5 bullet points."
  Cost: ~$0.001

Stage 2 (full model - gpt-4o):
  Input: head + middle_summary + tail
  Prompt: "Provide a comprehensive summary of this customer call."
  Cost: ~$0.01 (instead of $0.05 with full transcript)
```text
```yaml
Savings per transcript:
  Full 30-page transcript to gpt-4o: ~$0.05
  Optimized (head + summary + tail): ~$0.011
  Savings per call: ~78%
  At 100 calls/day: $150/month saved
```text
Always profile your actual token usage. Add a logging step that records input tokens, output tokens, and cost per LLM call so you can identify the most expensive workflows.

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