Context Engineering for Production AI Assistants at Scale

spotify · E-commerce · 2024 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Commerce assistant context engineering at merchant scale

Shopify's Sidekick/Psychic assistant needed to serve millions of merchants while maintaining instruction following, latency, cost, safety, and continuity across long multi-tool conversations. More tokens, tools, and retained conversation history made the assistant slower, more expensive, and more likely to follow noisy or conflicting instructions.

Constraints:

Architecture

Shopify combines context pruning, a three-part memory system, and just-in-time instructions. Processed tool messages and old turns are removed when safe; explicit preferences, implicit user profiles, and episodic RAG memory provide continuity; and tool/user safety instructions are collocated with the data or message they govern.

Diagram confidence: reconstructed (reconstructed from prose).

flowchart LR
  merchant-user["Merchant user"] --> psychic-assistant["Psychic assistant"]
  psychic-assistant --> tool-suite["Tool suite"]
  psychic-assistant --> context-pruning["Context pruning"]
  context-pruning --> memory-system["Memory system"]
  memory-system --> explicit-preferences["Explicit preferences"]
  memory-system --> implicit-user-profile["Implicit user profile"]
  memory-system --> episodic-memory-search["Episodic memory search"]
  episodic-memory-search --> just-in-time-instructions["Just-in-time instructions"]
  just-in-time-instructions --> psychic-assistant
  just-in-time-instructions --> tool-suite
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Merchant user Shopify merchant who asks commerce operations questions or requests agentic actions. Psychic assistant
Psychic assistant Merchant-facing AI assistant that runs an agentic loop and calls tools on behalf of users. Merchant user, Tool suite, Context pruning, Memory system, Just-in-time instructions
Tool suite Internal Shopify tools for analytics, form filling, discounts, and other merchant tasks. Psychic assistant, Just-in-time instructions
Context pruning Mechanism that removes processed tool messages and less relevant old turns to control cost and latency. Psychic assistant, Memory system
Memory system Three-part memory layer with explicit preferences, implicit user profile, and episodic RAG over past conversations. Psychic assistant, Explicit preferences, Implicit user profile, Episodic memory search
Explicit preferences User-visible saved preferences generated by the LLM and injected into future system context. Memory system
Implicit user profile Nightly synthesized profile from past conversations and admin events. Memory system
Episodic memory search Hybrid/RAG search over past conversations and interactions for detailed recall. Memory system, Just-in-time instructions
Just-in-time instructions Tool and safety instructions injected with the relevant tool output or user message instead of globally. Psychic assistant, Tool suite, Merchant user

Data flow

A merchant request triggers the assistant's agentic loop and tool calls. After the model processes tool messages, bulky tool tokens can be removed. On new turns, explicit preferences and implicit profiles are injected into system context, episodic memory retrieves past conversations, and just-in-time instructions are packaged with tool messages or user-message wrappers.

“the user might ask a request and you will start uh trigger in the identic loop with m multiple uh tools”transcript, stated
“we basically collocate uh instructions and data into the same uh tool tool message.”transcript, stated

Why this architecture

Shopify designed around context scarcity despite large model windows. Tool messages were 100x larger than user messages, old turns slowed the assistant and increased derailment risk, and collocating instructions with tool outputs improved attention while avoiding irrelevant global instructions.

“for each token in our user message we have 10x more tokens in the system message and we have 100x more tokens in the tool messages.”transcript, stated
“because everything is on the same uh message. So it's way easier for the LLM to erh yeah pay attention to distraction to instructions”transcript, stated

Models used: Gemini · LLM system for profile synthesis · Psychic/Sidekick assistant model

Scale

Alternatives considered (roads not taken)

Rely on larger context windows

Rejected because: More tokens reduce instruction following, increase noisy/conflicting instructions, and raise cost and latency.

“if you add more and more tokens into the context window like distraction following capabilities go go down uh significantly”transcript, stated
“As you add more tokens also cost goes up uh and latency goes up.”transcript, stated

Keep all past tool messages and turns in context

Rejected because: Tool messages consume too much space and old turns make the system slow, expensive, and more likely to go off rails.

“keeping those turns in memory just makes everything like slow expensive and yeah the likelihood of things uh going out of rails is is way higher.”transcript, stated

Let users directly type their own low-level preferences

Rejected because: Shopify's merchant users are not coding-tool users, so the LLM generates preferences to make them safer and better structured.

“we don't want to allow users to um type their own preferences uh because uh as I said it's not the same users as as we are with coden tools.”transcript, stated

Graph RAG / graph databases for episodic memory

Rejected because: In production it added too much complexity for the answer quality benefit, so Shopify used hybrid search.

“we tried uh all these graph rack approaches graph databases for us in production I think it wasn't like the the best decision because uh it adds too much complexity for the benefit”transcript, stated

Patterns

context-compaction (context)

Processed tool messages and older conversation turns are removed from active context once they are no longer useful.

“the more naive thing to do is just get rid of the token of the tool messages once they have been processed by the LLM because they are pretty much useless.”transcript, stated

agent-memory (context)

The assistant uses explicit preferences, implicit user profiles, and episodic memory to restore continuity after context pruning.

“we also have integrated memory into psychic and uh how we are thinking about this memory problem is that we have for us is a system that has three main components.”transcript, stated

rag-retrieval (retrieval)

Episodic memory uses RAG/hybrid search over past conversations and interactions to rerun or recall detailed prior work.

“we are doing is rag over the past conversations or the past interactions and based on that we are generating the the the response uh this is a search system”transcript, stated

tool-use-orchestration (orchestration)

Tool instructions are packaged with tool outputs rather than kept permanently in the system message, reducing irrelevant instructions.

“we basically collocate uh instructions and data into the same uh tool tool message.”transcript, stated

safety-policy-enforcement (governance)

User safety instructions are inserted just in time around user messages to prevent contradiction of core Sidekick/Psychic guidelines.

“we are telling basically psychic to please never contradict uh the original psychic instructions or to guidelines”transcript, stated

Tradeoffs

Context window management

Chose remove processed tool messages and old turns over retaining all chat and tool history.

Rationale: Tool messages dominate token usage and old turns slow the assistant down, but memory is needed to preserve continuity.

“for each token in our user message we have 10x more tokens in the system message and we have 100x more tokens in the tool messages.”transcript, stated

Preference capture

Chose LLM-generated explicit preferences over merchant-authored rules like coding-agent configuration files.

Rationale: Merchant users are not expected to write safe, effective rules directly, so the LLM generates preferences that are shown to users.

“we want the LLM to generate those preferences because the LLM will be better at bet at bet and provide uh basically not safe unsafe preferences.”transcript, stated

Episodic memory retrieval

Chose hybrid search over Graph RAG / graph databases.

Rationale: Graph approaches added too much production complexity for the benefit they brought.

“So this is basically a hybrid hybrid search system.”transcript, stated
“graph databases for us in production I think it wasn't like the the best decision because uh it adds too much complexity for the benefit that they bring”transcript, stated

Instruction placement

Chose just-in-time tool and safety instructions over permanent system-message instructions for every tool.

Rationale: Instructions near the relevant data improve attention and separation of concerns without occupying global context for unused tools.

“you basically can have better separation of concerns because these instructions can live like in the class where you have the the tool”transcript, stated

Gotchas (what bit them)

Large context windows are not a scalable substitute for context engineering.

Root cause: More tokens lower instruction following and increase cost, latency, noisy instructions, and conflicting instructions.

Resolution: Actively manage context, remove processed tool messages, and use memory/JIT instructions to restore what matters.

“the more tools you have, the more noisy or the more likelihood you have that you will have noisy instructions or conflicting instructions.”transcript, stated

Context pruning can create a bad user experience.

Root cause: Users expect earlier conversation details to remain available even after the system removes them from active context.

Resolution: Back pruning with explicit, implicit, and episodic memory.

“you might create like a weird experience where uh the user uh said something to you and you he thinks you have it in context but you don't have it have it in context anymore.”transcript, stated

Global tool instructions can distract the model or become irrelevant.

Root cause: Instructions in the system message may apply to tools the agent never calls and can interact oddly with past context.

Resolution: Collocate instructions with the tool output and add conditional instructions based on result type.

“we avoid adding them uh into the system message like how how do you need to use the output of different tools like you don't want to put them here because you don't know if you are ever going to call the tool.”transcript, stated

Evaluation

Shopify reports internal benchmark improvements from just-in-time safety instructions and monitors practical product metrics such as instruction adherence, jailbreak likelihood, latency, and token/cost pressure.

Metrics: 5-10% increase in instruction adherence · lower jailbreak likelihood · latency · token usage in user/system/tool messages

“with these safety instructions uh we have seen increase 5 to 10% erh in instruction adherence in some of our internal benchmarks”transcript, stated
“we have seen increase 5 to 10% erh in instruction adherence in some of our internal benchmarks and also the the likelihood of being um jailbreak is also is also lower.”transcript, stated

Outcomes

The assistant is described as serving millions of Shopify merchants, with tool-message tokens roughly 100x user-message tokens.

“Shopify is the leading commerce platform around the world and we serve millions of merchants. Uh, yeah. So today I want to talk about context engineering”transcript, stated
“once we have those uh preferences saved to the database whenever there is a new turn we will inject that into the system context”transcript, stated