Making the Most of Small Context Windows in Local LLMs

I maintain a WordPress plugin called AI Assistant (which you can try at my.wordpress.net) that allows you to work with your WordPress using local LLMs.

This also works when your WordPress site is hosted elsewhere: while typical implementations of local AI in WordPress expect the site to be on your localhost so that it can talk to the LLM server-side, AI Assistant runs in JavaScript. This allows it to use browser-side HTTP requests to localhost to talk to LM Studio or Ollama.

While AI Assistant supports talking to cloud providers through WordPress’s AI Connectors, I wanted to make it work well with local LLMs, which comes with their own challenges.

An important one is the relatively small context window. Local LLMs often run on hardware that simply doesn’t have as much memory as the servers running models in the cloud. The larger the context window, the more memory the model needs. So even if a model supports a large context window, you might only be able to use a fraction of it on your machine.

Over time I have introduced a number of strategies that I have now compiled a document inside the AI Assistant repository: Working with Local LLMs. I recommend you to read it!

Conventional approach

AI Assistant’s approach

A few highlights from the guide

These are some of the strategies I’ve implemented in AI Assistant. The full guide goes into more detail.

Keep the system prompt concise and stable

Every token in the system prompt has an initial impact on the context window already, so keep it short, but don’t change it between requests.

In anticipation of the user wanting to interact with the abilities of other plugins, they can drop short topic hints via WordPress filter.

Keep large tool responses out of the main context

It can happen that a tool call returns a response that is unexpectedly long for the model, for example a long blog post. In such a case, AI Assistant will send a short preview, keep the tool result in memory, and allow the model to query it from there via further tool calls.

Handle repeated tool calls gracefully

It can happen that the local model re-requests the same tool call again and again because its context overflows. The default strategy of LM Studio is to “truncate middle” which then makes the model ask again because the first response was malformed.

In such a case, AI Assistant will pretend the second tool call didn’t happen and modify its first tool call result with a compact one like above, giving the model a chance to work with an untruncated response.

Stop when the model gets stuck in a loop

Finally, if nothing helps, AI Assistant stops when the model ends up in some sort of loop. Sometimes, all the tooling can’t help when the constraints of the model make it impossible for it to fulfil the task.

If you want to read more about this, check the doc Working with Local LLMs inside the AI Assistant repository.

Fediverse reactions
Alex Kirk
Alex Kirk
@alex@kirk.at
275 posts
295 followers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.