How to use LLM for efficient text outputs longer than 4k tokens?
Tags: llm, streaming, ai, patch
Published: 2024-06-10 12:09:16

---
title: How to use LLM for efficient text outputs longer than 4k tokens?
published: true
description: If you ask an LLM to improve your text, it will reply with the whole text. This is time consuming and expensive. In this article we discuss how to use patching prompts and diffs to modify texts efficiently.
tags: LLM, streaming, AI, patch
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-06-15 20:07 +0000
---


Hey community,

I wanted to share a nifty tool that could save us a lot of time and cost when we're making edits to large texts. It's called **LLM Patcher**, and it's built with Next.js, the Vercel AI SDK, and OpenAI.

## The Problem: Time and Cost Inefficiencies

You know how painful it can be when we ask an LLM to make changes to a large document? It always sends back the entire modified text. For big files, this means:

1. **Time Drain**: We end up waiting for the whole text to stream back, which is super slow.
2. **Cost Spike**: It’s expensive because we’re processing and transferring more data than needed.

LLM Patcher is designed to tackle these issues head-on.


## Demo

![How LLM Patcher works Demo Video](https://github.com/theluk/llm-patcher/raw/main/public/demo.gif)

## How LLM Patcher Works

Here’s a quick rundown of how it streamlines the process:

1. **Input and Query**: You feed it the text and a find-and-replace query.
2. **Text Segmentation**: It splits the text into lines and sentences.
3. **Identifier Prefixing**: Each segment gets a unique identifier (like `<l1s1>` for line 1, sentence 1).
4. **Find-and-Replace Execution**: The LLM processes each segment to apply the changes.
5. **Streaming Changes**: Instead of the whole text, it streams back just the changes in a diff format (e.g., `<r:l1s1> string to find || string to replace`).

## What LLM Patcher Is and Isn’t For

### What It’s Great For:

- Finding and replacing text in large documents.
- Editing and patching texts efficiently.
- Specific workflows like:
  - Fixing typos or word replacements.
  - Anonymizing texts by replacing names.
  - Handling sensitive info by replacing it with placeholders.
  - Enhancing texts with SEO keywords.

### What It’s Not Designed For:

- Generating text from scratch.
- Summarizing or translating text.
- Sentiment analysis.
- Acting as a chatbot.
- General-purpose LLM tasks.

## Getting Started

Setting it up locally is straightforward:

1. Clone the repo and navigate to the project directory.
2. Set up your environment variables as defined in `.env.example` for OpenAI integration.
3. Install dependencies and start the dev server:

   ```bash
   pnpm install
   pnpm dev
   ```

4. The app should now be live on [localhost:3000](http://localhost:3000/).

> **Heads Up**: Don’t commit your `.env` file – it contains sensitive info.

## Final Thoughts

LLM Patcher could be a real game-changer for a lot of workflows, especially when dealing with large text edits. It’s efficient, cost-effective, and tailored for find-and-replace operations. Check out the [LLM Patcher Repository](https://github.com/theluk/llm-patcher) to see it in action. You can also checkout out the live demo inside the repo!

Let me know if you want to dive deeper into this or have any questions about integrating it into our current projects.