Quest Lab · White paper · December 2024

Engineering generative AI applications. From use case to guardrails.

December 2024By Muhammad Zohaib Iqbal, PhD6 min read

Summary

Generative AI applications are software, and they need software engineering. But they are software of an unusual kind: probabilistic, data-driven, built on models that change underneath them, and exposed to new kinds of attack. This paper sets out how we approach engineering them, from choosing a feasible use case and designing the solution, through architecture and development practice, to the guardrails, security measures and ethical commitments that let an application run safely in production.

Why AI needs software engineering

Prototypes of generative AI applications are easy to build. The common experience is that the application works while prototyping and becomes flaky in production. The reasons are familiar to any engineer, made sharper by AI:

  • Data: availability, quality, volume, and the ethics and privacy of how it is used.
  • Model accuracy: and, for generative AI, keeping output creative enough to be useful while relevant enough to be trusted.
  • Computational resources: training, fine-tuning and inference all cost money and time.
  • A moving foundation: models sit behind APIs, and their versions can change without warning.

The answer is to treat AI development as an engineering discipline, with the same rigor applied to any serious system and some new practices on top.

Start with the use case, not the technology

Generative AI is a great equalizer: almost every business, large or small, can benefit from it, with an order-of-magnitude gain in scalability and efficiency. That makes the first question critical: where to start?

Identify feasible use cases. Look for opportunities in operational efficiency, customer experience and supporting excellence, and separate hype from reality. For each candidate, assess:

  • AI readiness: is the data and the organization ready?
  • Feasibility: can AI do this task reliably today?
  • Possible integrations: which existing systems must it connect to?

Identifying the infeasible cases is as valuable as finding the feasible ones. It saves budget and credibility.

Understand the domain. Define the problem, gather the data and set objectives before choosing tools. A domain model, mapping the key concepts and how they relate, keeps the solution grounded in how the business actually works.

Designing the solution

A sound generative AI solution design has six parts:

  1. User journey: each step a user takes and how the system responds. For a university programs chatbot, that runs from the greeting, through queries, category selection, program details and follow-up information, to optional lead collection and feedback. Journeys can be written as text or drawn as flowcharts.
  2. Architecture: external integrations, storage, components, front-end technology and internal layers.
  3. Wireframes: what the user will see.
  4. Technology selection: models, frameworks and platforms chosen for the project's needs, not for fashion.
  5. Delivery plan.
  6. Deployment plan.

Architecture and development practice

Architecture. The same principles that guide any system apply: choose patterns, such as microservices or a monolith, to fit the project's requirements. For generative AI, a layered approach works well:

  • a data layer for sources, pipelines and vector stores,
  • a model layer for the LLMs and other models, and how they are accessed,
  • an application layer for the user experience and business logic,

with deliberate integration into the systems the organization already runs. Modular design and reusable components keep the architecture maintainable as models and tools change.

Development practice. Best practices from software engineering carry over and need extending:

  • Modular design, version control, testing and deployment for code, and also for data, prompts and models.
  • Data pipelines for preprocessing and augmentation, with data ethics and privacy designed in.
  • Model development and optimization: choosing the right architecture, optimizing performance, and CI/CD pipelines for AI models.
  • Scalability and performance: designing for scale from the start, using cloud computing and distributed systems.
  • Collaboration between data scientists and software engineers, who bring different and equally necessary skills.

Controlling LLMs: guardrails

LLMs are powerful but brittle and hard to control. Getting correct output every time is difficult: hallucinations, falsehoods and broken structure all appear in production.

The two obvious levers are weak. Prompts offer no guarantees: LLMs are stochastic, the same input does not always give the same output, and models do not always follow instructions. Training or fine-tuning a model on custom data is expensive and slow, and models behind APIs can change version without notice.

What works is to combine LLMs with verification:

  • Output verification: application-specific checks that confirm the output is correct before it is used.
  • Input moderation: checks on what goes in, especially where the restricted topics in a context are known.

A production application places rails at four points: input rails, retrieval rails, generation rails and output rails. Three techniques implement them:

  1. Rule-based checks: limits on text size, forbidden words, and masking confidential information such as phone numbers, email addresses and bank accounts.
  2. LLM-based metrics: using models to measure properties of text, such as semantic similarity between a summary and its source to confirm accuracy and relevance, and monitoring how often guardrails are triggered.
  3. LLM judges: fine-tuned models that evaluate text against more nuanced criteria, for example whether content is toxic or safe.

Securing generative AI

Generative AI applications face attacks that conventional software does not:

  • Jailbreaks: deliberate, manipulative attempts to bypass an application's safety filters.
  • Prompt injection: hidden instructions in user input or retrieved content that hijack the model's behavior.
  • Adversarial attacks: small, carefully crafted changes to input that make a model produce wrong output, and model inversion, where repeated queries reconstruct sensitive training data.
  • Data poisoning: malicious data injected into training, which subtly changes behavior. It is especially damaging for systems that keep learning after deployment, as a well-known chatbot that learned from social media users showed within hours of launch.
  • Model stealing: reconstructing a working copy of a model from its outputs, undermining the investment in it.
  • Confidence reduction: queries designed to make a model uncertain. Where confidence drives action, as in medical diagnosis or autonomous vehicles, this can paralyze a system.

Security has to be designed in from the first sprint, not added before launch.

Responsible AI

AI already shapes decisions about people: their access to news and information, creditworthiness, CV screening, job performance, visa screening and medical diagnosis. Engineering responsibly means building for:

  • Fairness. An AI is only as fair as the data it learns from. Training data reflects society's existing biases, and AI can amplify them; one widely reported hiring tool favored male candidates. Seemingly neutral algorithms can reinforce inequality, and models can fail badly for minorities simply because there is too little data about them. Bias in training data has to be assessed and countered.
  • Transparency. People affected by an AI system, and those who use it, should be able to understand how it reached a decision. A healthcare tool should be able to explain to a doctor how it arrived at a diagnosis. This is the goal of explainable AI.
  • Accountability. When an AI system fails or causes harm, who is responsible: the manufacturer, the developer or the user? Large teams and dynamic environments make this hard, which is why it must be settled by design.
  • Privacy. Informed consent, data minimization, anonymization and care over secondary use of data.
  • Intellectual property. Questions of copyright in training data, authorship of AI-generated work, and an evolving legal landscape.
  • Authenticity. Deepfakes and realistic but misleading content challenge identity and trust.
  • Humanity. Some processes, such as a job interview conducted by a machine, risk dehumanizing decisions that matter to people.

Conclusion

Generative AI does not remove the need for software engineering; it raises the bar. Successful applications start from a feasible use case and a well-understood domain, follow a deliberate solution design and a layered architecture, extend proven development practice to data, prompts and models, and wrap the model in guardrails, security and responsible design. That is how a promising prototype becomes a system people can rely on.

Quest is a frontier AI lab. This paper reflects our perspective as of December 2024.

Designing a generative AI application?Tell us the use case and we'll talk through the architecture.
info@questlab.pk →