The Complexities and Challenges of Integrating LLMs into Functions | by Shahar Davidson | Jul, 2023

Planning to combine some LLM service into your code? Listed below are a number of the frequent challenges it is best to anticipate when doing so

Picture by Christina @ on Unsplash

Massive Language Fashions (LLMs) existed earlier than OpenAI’s ChatGPT and GPT API had been launched. However, due to OpenAI’s efforts, GPT is now simply accessible to builders and non-developers. This launch has undoubtedly performed a major position within the latest resurgence of AI.

It’s actually exceptional how shortly OpenAI’s GPT API was embraced inside simply six months of its launch. Nearly each SaaS service has integrated it not directly to extend its customers’ productiveness.

Nonetheless, solely those that have accomplished the design and integration work of such APIs, genuinely perceive the complexities and new challenges that come up from it.

Over the previous few months, I’ve carried out a number of options that make the most of OpenAI’s GPT API. All through this course of, I’ve confronted a number of challenges that appear frequent for anybody using the GPT API or some other LLM API. By itemizing them out right here, I hope to assist engineering groups correctly put together and design their LLM-based options.

Let’s check out a number of the typical obstacles.

Contextual Reminiscence and Context Limitations

That is most likely the most typical problem of all. The context for the LLM enter is restricted. Only in the near past, OpenAI launched context help for 16K tokens, and in GPT-4 the context limitation can attain 32K, which is an efficient couple of pages (for instance if you’d like the LLM to work on a big doc holding a few pages). However there are lots of instances the place you want greater than that, particularly when working with quite a few paperwork, every having tens of pages (think about a legal-tech firm that should course of tens of authorized paperwork to extract solutions utilizing LLM).

There are completely different techniques to beat this problem, and others are rising, however this might imply you should implement a number of of those methods your self. Yet one more load of labor to implement, check and keep.

Knowledge Enrichment

Your LLM-based options seemingly take some kind of proprietary information as enter. Whether or not you might be inputting consumer information as a part of the context or utilizing different collected information or paperwork that you simply retailer, you want a easy mechanism that can summary the calls of fetching information from the assorted information sources that you simply personal.


The immediate you undergo the LLM will comprise hard-coded textual content and information from different information sources. Which means that you’ll create a static template and dynamically fill within the blanks with information that must be a part of the immediate in run-time. In different phrases, you’ll create templates to your prompts and sure have multiple.

Which means that you have to be utilizing some sort of templating framework since you most likely don’t need your code to seem like a bunch of string concatenations.

This isn’t an enormous problem however one other job that must be thought of.

Testing and Positive-tuning

Getting the LLM to succeed in a passable degree of accuracy requires lots of testing (typically it’s simply immediate engineering with lots of trial and error) and fine-tuning primarily based on consumer suggestions.

There are after all additionally checks that run as a part of the CI to claim that each one integration work correctly however that’s not the actual problem.

Once I say Testing, I’m speaking about working the immediate repeatedly in a sandbox to fine-tune the outcomes for accuracy.

For testing, you’ll desire a methodology by which the testing engineer may change the templates, enrich them with the required information, and execute the immediate with the LLM to check that we’re getting what we wished. How do you arrange such a testing framework?

As well as, we have to consistently fine-tune the LLM mannequin by getting suggestions from our customers relating to the LLM outputs. How can we arrange such a course of?


LLM fashions, comparable to OpenAI’s GPT, have a parameter to regulate the randomness of solutions, permitting the AI to be extra inventive. But if you’re dealing with requests on a big scale, you’ll incur excessive expenses on the API calls, you might hit price limits, and your app efficiency may degrade. If some inputs to the LLM repeat themselves in numerous calls, you might think about caching the reply. For instance, you deal with 100K’s calls to your LLM-based characteristic. If all these calls set off an API name to the LLM supplier, then prices will probably be very excessive. Nonetheless, if inputs repeat themselves (this may doubtlessly occur once you use templates and feed it with particular consumer fields), there’s a excessive probability which you could save a number of the pre-processed LLM output and serve it from the cache.

The problem right here is constructing a caching mechanism for that. It’s not onerous to implement that; it simply provides one other layer and shifting half that must be maintained and executed correctly.

Safety and Compliance

Safety and privateness are maybe probably the most difficult facets of this course of — how can we be certain that the method created doesn’t trigger information leakage and the way can we be certain that no PII is revealed?

As well as, you will have to audit all of your actions so that each one the actions might be examined to make sure that no information leak or privateness coverage infringement occurred.

It is a frequent problem for any software program firm that depends on third celebration providers, and it must be addressed right here as nicely.


As with every exterior API you’re utilizing, you should monitor its efficiency. Are there any errors? How lengthy does the processing take? Are we exceeding or about to exceed the API’s price limits or thresholds?

As well as, it would be best to log all calls, not only for safety audit functions but in addition that can assist you fine-tune your LLM workflow or prompts by grading the outputs.

Workflow Administration

Let’s say we develop a legal-tech software program that legal professionals use to extend productiveness. In our instance, now we have an LLM-based characteristic that takes a shopper’s particulars from a CRM system and the final description of the case labored on, and offers a solution for the lawyer’s question primarily based on authorized precedents.

Let’s see what must be executed to perform that:

  1. Lookup all of the shopper’s particulars primarily based on a given shopper ID.
  2. Lookup all the small print of the present case being labored on.
  3. Extract the related data from the present case being labored on utilizing LLM, primarily based on the lawyer’s question.
  4. Mix all of the above data onto a predefined query template.
  5. Enrich the context with the quite a few authorized instances. (recall the Contextual Reminiscence problem)
  6. Have the LLM discover the authorized precedents that greatest match the present case, shopper, and lawyer’s question.

Now, think about that you’ve 2 or extra options with such workflows, and eventually attempt to think about what your code appears like after you implement these workflows. I guess that simply desirous about the work to be executed right here makes you progress uncomfortably in your chair.

In your code to be maintainable and readable, you will have to implement varied layers of abstraction and maybe think about adopting or implementing some kind of workflow administration framework, in case you foresee extra workflows sooner or later.

And at last, this instance brings us to the subsequent problem:

Sturdy Code Coupling

Now that you’re conscious of all of the above challenges and the complexities that come up, you might begin seeing that a number of the duties that should be executed shouldn’t be the developer’s accountability.

Particularly, all of the duties associated to constructing workflows, testing, fine-tuning, monitoring the outcomes and exterior API utilization might be executed by somebody extra devoted to these duties and whose experience shouldn’t be constructing software program. Let’s name this persona the LLM engineer.

There’s no cause why the LLM workflows, testing, fine-tuning, and so forth, could be positioned within the software program developer’s accountability — software program builders are specialists at constructing software program. On the similar time, LLM engineers must be specialists at constructing and fine-tuning the LLM workflows, not constructing software program.

However with the present frameworks, the LLM workflow administration is coupled into the codebase. Whoever is constructing these workflows must have the experience of a software program developer and an LLM engineer.

There are methods to do the decoupling, comparable to making a dedicate micro-service that handles all workflows, however that is one more problem that must be dealt with.

A Spotify Knowledge Scientist’s Information to Turning Your Insights into Impactful Actions | by Khouloud El Alami | Jul, 2023

How one can *Not* Get Machine Studying Fashions in Manufacturing | by Eirik Berge | Jul, 2023