DEV Community

Programming Central
Programming Central

Posted on

Why Your LLM Architecture is Flawed: Mastering Dot-and-Index Paths and Context Isolation in Jev

If you are building production AI systems, you have likely hit a frustrating wall. Your prompts are meticulously engineered, your retrieval-augmented generation (RAG) pipeline is fully operational, and yet your model still hallucinates, drifts off-topic, or gives wildly uncalibrated answers when faced with complex, multi-tenant payloads.

The problem isn't your prompt. The problem is your state architecture.

In early tutorials, passing a single customer message or a short transaction string into an AI model works seamlessly. But production systems don't handle single sentences. They handle massive JSON projections containing orders, users, historical messages, policies, feature flags, and latent workflow states.

If you treat your state as a passive "input payload" rather than a strict, traversable API contract, you are opening your application up to context rot, prompt injection, and statistical dilution.

Let's fix that.


The Theoretical Foundations: State as an API Contract

To build reliable, sub-100ms decision loops with Jev, you need to understand the fundamental asymmetry of a System One architecture: The state is shared, but the questions are isolated.

When you submit a request to Jev, you pass a single state object alongside a collection of independent questions. Every question evaluates against that same state in parallel. No question sees another question's answer. The state is the only channel through which information flows.

Because of this, the state is not a neutral container. It is the terrain over which every judgment walks.

The DOM and Filesystem Analogies

Think of your state object like the Document Object Model (DOM) in front-end development. You don't interact with a web page by dumping its raw text into a black box; you interact with it through a structured, addressable tree using CSS selectors.

Jev state works identically. When you write a question that targets ticket.messages[0].text, you are writing a selector. You are describing an absolute path through a structured object by walking named keys and numeric indices.

Similarly, think of a filesystem path like /var/log/nginx/access.log. The hierarchy encodes semantics and access models. A path like order.charges[0].status coerces the model's attention. It tells the reader—before a single byte of reasoning occurs—that this query is about a charge, it is the first charge in a list, and we are asking about its status, not its timestamp or amount.

Path specificity is inversely proportional to inference cost. The more completely a path specifies its target, the less the model must guess, and the more concentrated its probability distribution will be around the correct answer.

The concepts and code demonstrated here are drawn directly from my ebook Jev: The Definitive Guide to System One AI here. Check also the 9 volumes bundle: TypeScript AI & Agentic Engineer Masterclass


The Principle of Shallow State and Schema Design

When designing your state schema, three competing priorities must be balanced: path clarity, update locality, and context isolation.

  1. Keep paths shallow: Deeply nested structures like workflow.stages[2].steps[5].outputs.results[0].data.amount force the model to maintain too many cognitive frames of reference. This increases traversal cost, lowers confidence scores, and broadens probability distributions. Aim for a maximum of three to four levels of depth.
  2. Design for immutable updates: In live applications, state changes continuously. If you mutate state objects in place, diffing becomes impossible, forcing you to re-run expensive queries unnecessarily. By leveraging immutable updates with structural sharing, only the modified branches of your state tree are replaced. This makes state diffing trivial and lets your caching layer decide which question families actually need re-evaluation.
  3. Enforce strict isolation: Jev is susceptible to prompt injection and adversarial content if hostile text is mixed into the same namespace as trusted system logic. Isolation must be enforced before the model runs, at the state schema level. Unreferenced content in a well-scoped state acts as neutral background context rather than contaminating the active target.

Anatomy of a Production Jev System

Let's look at a complete, end-to-end TypeScript program that puts these theoretical foundations into practice. This billing triage example handles a nested JSON state, uses backticked dot-and-index paths, evaluates three primitive types (noul, choice, and score) in a single parallel request, and composes the final business logic entirely in code.

// src/lib/triage.ts
import { TypeSafeClient, choice, noul, score } from "@typesafe-ai/sdk";

/**
 * One per-process client. Reads `TYPESAFE_API_KEY` from the environment
 * and defaults to `jev-latest`. Reuse this object across requests so
 * the connection pool and retry policy stay warm.
 */
const client = new TypeSafeClient();

/**
 * The `state` is the single blob of evidence the model reads to answer
 * every question. Paths *inside* this object are the addressing surface
 * that the questions point at, using backticked dot-and-index
 * expressions such as `ticket.messages[0].text`.
 */
const state = {
  ticket: {
    id: "T-1042",
    messages: [
      {
        from: "customer",
        text: "I was charged twice for order A-104. Please refund the duplicate.",
      },
      {
        from: "support",
        text: "We are checking the charges.",
      },
    ],
  },
  order: {
    id: "A-104",
    charges: [
      { amount_usd: 49, status: "captured" },
      { amount_usd: 49, status: "captured" },
    ],
  },
  refund_policy: "Duplicate charges are eligible for a refund.",
};

/**
 * Every question is defined statically. The keys (`refund_requested`, 
 * `department`, etc.) are what the answers are keyed by in the response.
 */
const questions = {
  refund_requested: noul(
    "Does `ticket.messages[0].text` request a refund?",
  ),
  duplicate_charge: noul(
    "Do `order.charges[0].amount_usd` and `order.charges[1].amount_usd` match?",
  ),
  policy_supports_refund: noul(
    "Given `refund_policy`, does it support the request in `ticket.messages[0].text`?",
  ),
  department: choice("Which team should handle `ticket.messages[0].text`?", {
    billing: "Payment or subscription issues",
    technical: "Bugs or integration problems",
    sales: "Pricing or account questions",
  }),
  frustration: score(
    "How frustrated is the customer in `ticket.messages[0].text`?",
    [
      "Calm and factual",
      "Frustrated but civil",
      "Very angry, strong language",
    ],
  ),
};

/**
 * Orchestrates one triage pass in a single systemOne call.
 */
export async function triage(): Promise<void> {
  const result = await client.systemOne({ state, questions });

  const refund = result.answers.refund_requested.noul;
  const duplicate = result.answers.duplicate_charge.noul;
  const policy = result.answers.policy_supports_refund.noul;
  const department = result.answers.department.choice;
  const frustration = result.answers.frustration.score;

  // Compose independent judgments into a deterministic business rule in code.
  const shouldAutoRefund =
    refund >= 0.8 && duplicate >= 0.8 && policy >= 0.8;

  console.log({
    department,
    frustration,
    shouldAutoRefund,
    probabilities: result.answers.department.probabilities,
    confidence: result.answers.department.confidence,
  });
}

triage().catch(console.error);
Enter fullscreen mode Exit fullscreen mode

Walking the Program Line by Line

1. The Client Lifecycle

Constructing new TypeSafeClient() once at the module level rather than inside your handler functions is critical. Reusing your client instance keeps your TLS setup, retry bookmarking, and keep-alive connection pools warm, preventing latency spikes in hot request paths.

2. Backticked Dot-and-Index Paths

Notice how the question instructions wrap paths in backticks: \ticket.messages[0].text``. This is not mere formatting; it is a formal directive to the model. It instructs Jev to treat the token as a structural coordinate rather than generic text. Omitting backticks leads to inconsistent path resolution and brittle behavior when surrounding text happens to match keywords in your schema.

3. The Power of Primitives (noul, choice, score)

  • noul: Returns a calibrated float between 0 and 1 representing a closed binary probability.
  • choice: Returns a typed union matching your criteria keys, alongside full probability distributions and confidence metrics. This allows you to inspect runner-ups and route ambiguous cases to human review queues.
  • score: Evaluates an ordered list of criteria and returns a probability-weighted mean score, capturing nuance and gradient rather than forcing a rigid binary category.

4. Code-Driven Business Rules

Notice where the decision logic lives: entirely in TypeScript code (refund >= 0.8 && duplicate >= 0.8 && policy >= 0.8). The model’s job is strictly to evaluate normalized evidence against precise coordinates. Your code's job is to apply business thresholds to those calibrated outputs.


Advanced Application: Request-Scoped Isolation in Next.js

When scaling to multi-tenant architectures handling tens of thousands of requests, simple JSON payloads are no longer enough. You need physical retrieval isolation, path-rendering utilities, and request-scoped state management.

Below is an advanced architecture utilizing Node's AsyncLocalStorage to guarantee that concurrent tenant operations never cross contaminate, alongside a server action that integrates Pinecone vector search with Jev state projection.

File 1 — State Shape and Selectors (lib/jev/state-shape.ts)

`typescript
export type PathSeg = string | number;
export type StatePath = readonly PathSeg[];

export function renderPath(path: StatePath): string {
return path.reduce(
(acc, seg) => (typeof seg === "number" ? acc[{acc}[ {seg}] : acc ? acc.{acc}. {seg} : seg),
"",
);
}

export const cite = (path: StatePath): string => \${renderPath(path)}``;

export function getAt(root: unknown, path: StatePath): unknown {
let node: unknown = root;
for (const seg of path) {
if (node === null || node === undefined) return undefined;
node = (node as Record)[seg];
}
return node;
}

export function setAt(root: T, path: StatePath, value: unknown): T {
if (path.length === 0) return value as T;
const [head, ...rest] = path;
if (Array.isArray(root)) {
const copy = root.slice();
copy[head as number] = setAt(copy[head as number], rest, value);
return copy as unknown as T;
}
const base = (root ?? {}) as Record;
return { ...base, [head]: setAt(base[head], rest, value) } as T;
}

export interface Candidate {
rank: number;
id: string;
similarity: number;
ageDays: number;
sourceType: "policy" | "contract" | "kb" | "forum";
title: string;
passage: string;
}

export interface ShapedState {
fastPath: {
entitlements: { plan: "free" | "pro" | "enterprise"; allowsAutoReply: boolean };
routing: { autoReplyFloor: number; escalateBelow: number; injectionCeiling: number };
};
task: { question: string; locale: string; channel: "email" | "chat" | "api" };
evidence: { index: string; namespace: string; topK: number; candidates: Candidate[] };
derived: { candidateCount: number; maxSimilarity: number; totalPassageChars: number; hasPolicySource: boolean };
}

export function project(state: ShapedState): Record {
return {
task: state.task,
evidence: state.evidence,
derived: state.derived,
};
}
`

File 2 — Request-Scoped Isolation (lib/jev/scope.ts)

`typescript
import { AsyncLocalStorage } from "node:async_hooks";
import { setAt, getAt, type ShapedState, type StatePath } from "./state-shape";

export const vectorNamespace = (tenantId: string): string => tenant:${tenantId};

export interface ScopeHandle {
readonly tenantId: string;
readonly requestId: string;
readonly state: ShapedState;
commit(path: StatePath, value: unknown): void;
at(path: StatePath): T;
}

const storage = new AsyncLocalStorage();

export function withScope(
init: { tenantId: string; requestId: string; fastPath: ShapedState["fastPath"] },
fn: (scope: ScopeHandle) => Promise,
): Promise {
let state: ShapedState = {
fastPath: init.fastPath,
task: { question: "", locale: "en", channel: "api" },
evidence: { index: "", namespace: vectorNamespace(init.tenantId), topK: 0, candidates: [] },
derived: { candidateCount: 0, maxSimilarity: 0, totalPassageChars: 0, hasPolicySource: false },
} as ShapedState;

const handle: ScopeHandle = {
tenantId: init.tenantId,
requestId: init.requestId,
get state() { return state; },
commit(path, value) { state = setAt(state, path, value); },
at(path: StatePath) { return getAt(state, path) as T; },
};

return storage.run(handle, () => fn(handle));
}

export function currentScope(): ScopeHandle {
const scope = storage.getStore();
if (!scope) throw new Error("No Jev scope open: call withScope() at the request boundary.");
return scope;
}
`

File 3 — The Server Action (app/actions/answer-from-evidence.ts)

`typescript
"use server";

import { Pinecone } from "@pinecone-database/pinecone";
import { choice, noul, score, RateLimitError, TypeSafeClient } from "@typesafe-ai/sdk";
import { passagePath, project, type Candidate } from "@/lib/jev/state-shape";
import { vectorNamespace, withScope } from "@/lib/jev/scope";

const JEV_MODEL = "jev-1.13.0";
const TOP_K = 12;
const INJECTION_SCAN_K = 6;

const jev = new TypeSafeClient({
apiKey: process.env.TYPESAFE_API_KEY!,
defaultModel: JEV_MODEL,
timeout: 4_000,
});

const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY! });
const index = pinecone.index(process.env.PINECONE_INDEX!);

export async function answerFromEvidence(input: {
tenantId: string;
requestId: string;
question: string;
locale: string;
channel: "email" | "chat" | "api";
plan: "free" | "pro" | "enterprise";
}) {
return withScope(
{
tenantId: input.tenantId,
requestId: input.requestId,
fastPath: {
entitlements: { plan: input.plan, allowsAutoReply: input.plan !== "free" },
routing: { autoReplyFloor: 0.85, escalateBelow: 0.55, injectionCeiling: 0.7 },
},
},
async (scope) => {
// 1. Retrieve inside the tenant's physical namespace
const vector = [0.1]; // Mock embedding vector
const { matches } = await index.namespace(vectorNamespace(scope.tenantId)).query({
vector,
topK: TOP_K,
includeMetadata: true,
});

  // 2. Shape and precompute scalars so Jev never does arithmetic
  const now = Date.now();
  const candidates: Candidate[] = matches.map((m, i) => ({
    rank: i + 1,
    id: String(m.id),
    similarity: Number((m.score ?? 0).toFixed(4)),
    ageDays: 2,
    sourceType: "kb",
    title: "Sample Doc",
    passage: String(m.metadata?.text ?? "").slice(0, 1_200),
  }));

  scope.commit(["task"], { question: input.question, locale: input.locale, channel: input.channel });
  scope.commit(["evidence"], { index: "idx", namespace: vectorNamespace(scope.tenantId), topK: TOP_K, candidates });

  // 3. Build questions targeting precise paths
  const core = {
    best_passage: choice(
      `Which passage in \`evidence.candidates\` states the answer to \`task.question\`?`,
      { ...Object.fromEntries(candidates.map((c) => [c.id, null])), none: "No matching passage." }
    ),
    evidence_sufficient: noul(
      `Do the passages in \`evidence.candidates\` contain everything needed to answer \`task.question\`?`
    ),
  };

  const questions = { ...core };

  // 4. Execute single parallel call
  const result = await jev.systemOne({ state: project(scope.state), questions, model: JEV_MODEL });

  return {
    success: true,
    answers: result.answers,
  };
}
Enter fullscreen mode Exit fullscreen mode

);
}
`


Common Pitfalls to Avoid in Production

  1. Forgetting Backticks: Writing paths like ticket.messages[0].text without backticks forces the model to guess whether you are writing code or natural language prose, causing massive confidence drops. Always wrap paths in backticks.
  2. Looping Over Questions or Requests: Never call systemOne inside a loop for individual questions or separate tickets. Batch your questions into a single state payload and fan out ticket requests using Promise.all().
  3. Delegating Math to the Model: Jev is an inference engine, not a calculator. Do not ask the model to count array items, compare dates, or sum billing totals. Precompute those values in TypeScript code and pass them as scalar fields in your state.
  4. Failing to Isolate Tenants: Relying on query filter strings ({ tenantId: currentTenant }) leaves your database exposed to human error. Use physical storage namespaces (like Pinecone namespaces) and AsyncLocalStorage to enforce isolation at the infrastructure level.

Conclusion

State shaping is ultimately API design. When you treat your Jev state object as a structured, addressable contract rather than a messy string dump, everything changes.

By keeping paths shallow, precomputing scalars in code, enforcing request-scoped isolation, and pointing questions at exact coordinate paths, you transform LLM output from a probabilistic gamble into a reliable, high-performance engineering primitive. Structure your terrain properly, and your model will deliver crisp, calibrated answers every single time.

Top comments (0)