Deep Agents
AgentContextOrchestratorRetrievalText2SQLToolbox

User Fragments

Fragment builders for user identity, preferences, and personalization

User fragments capture who the user is, how they want responses delivered, and corrections they have made. These fragments tailor AI responses to individual users without modifying domain knowledge.

Identity

identity(input)

Defines the user's name and/or role. Helps the AI tailor explanations and terminology to the user's perspective.

import { identity } from '@deepagents/context';

identity({ name: 'John', role: 'VP of Sales' });
identity({ role: 'Data analyst in the marketing team' });
identity({ name: 'Sarah' });
ParameterTypeRequiredDescription
namestringNoThe user's name
rolestringNoThe user's role or position

persona(input)

Configures the AI assistant's identity, expertise, and communication style.

import { persona } from '@deepagents/context';

persona({
  name: 'DataBot',
  role: 'SQL Expert',
  objective: 'Generate accurate SQL queries from natural language',
  tone: 'professional',
});

persona({ name: 'QueryMaster', role: 'Database Analyst' });
ParameterTypeRequiredDescription
namestringYesThe persona's name
rolestringNoThe persona's expertise or identity
objectivestringNoWhat the persona should accomplish
tonestringNoCommunication style (e.g., friendly, professional, concise)

Vocabulary

alias(term, meaning)

Defines user-specific vocabulary overrides. When a user calls something by a non-standard name, alias maps their language to the correct meaning.

import { alias } from '@deepagents/context';

alias('the big table', 'the orders table');
alias('revenue', 'gross revenue before deductions, not net');
alias('Q4', 'October through December, not fiscal Q4');
ParameterTypeRequiredDescription
termstringYesThe term the user uses
meaningstringYesWhat the user means by this term

alias vs term

Both deal with vocabulary, but they serve different purposes:

alias()term()
ScopePer-userDomain-wide
PurposeRename: maps user language to correct meaningDefine: explains domain terminology
Examplealias('the big table', 'the orders table')term('NPL', 'non-performing loan')

Use term() to teach the system what "NPL" means across all users. Use alias() when one user says "the big table" but means orders.

Preferences

preference(aspect, value)

Declares output formatting or behavioral preferences that apply across all interactions with this user.

import { preference } from '@deepagents/context';

preference('date format', 'YYYY-MM-DD');
preference('output style', 'tables over charts unless trend data');
preference('detail level', 'always show the SQL query in responses');
preference('row limit', 'default to 50 rows unless I ask for more');
ParameterTypeRequiredDescription
aspectstringYesWhat aspect of output this preference applies to
valuestringYesThe user's preference

Learning

correction(subject, clarification)

Records a user correction to prevent the same mistake from happening again. Use this when the user corrects a misunderstanding about data, columns, or business logic.

import { correction } from '@deepagents/context';

correction('status column', '1 = active, 0 = inactive, not boolean true/false');
correction(
  'orders table',
  'Use orders_v2, not the deprecated legacy_orders table',
);
correction('revenue calculation', 'Must exclude refunds and chargebacks');
ParameterTypeRequiredDescription
subjectstringYesWhat was misunderstood
clarificationstringYesThe correct understanding

Combined Example

Here is a full setup using ContextEngine with multiple user fragments:

import {
  ContextEngine,
  SqliteContextStore,
  XmlRenderer,
  alias,
  correction,
  identity,
  persona,
  preference,
  role,
  user,
} from '@deepagents/context';

const store = new SqliteContextStore('./chat.db');
const context = new ContextEngine({
  store,
  chatId: 'chat-001',
  userId: 'user-001',
}).set(
  role('You are a data assistant.'),
  identity({ name: 'Sarah', role: 'Finance manager' }),
  persona({ name: 'Freya', role: 'Data Assistant', tone: 'professional' }),
  alias('revenue', 'gross revenue before deductions'),
  alias('the big table', 'the orders table'),
  preference('date format', 'YYYY-MM-DD'),
  preference('output style', 'tables over charts'),
  correction('status column', '1 = active, 0 = inactive'),
);

context.set(user('Show me revenue by region for Q4'));

const { systemPrompt, messages } = await context.resolve({
  renderer: new XmlRenderer(),
});

Next Steps