Skip to content

Implement Instruction Augmentation

Learn how to implement Instruction Augmentation (Semantic Directives), a powerful system for dynamically enhancing agent prompts based on scope and context, enabling contextual, personalized AI interactions.

By the end of this guide, you will:

  • Enable instruction augmentation feature
  • Create semantic directives via API or infrastructure as code
  • Understand scope patterns and targeting
  • Integrate with entity-based personalization
  • Manage directives through admin interface
  • Optimize performance and selection
  • A running Pika installation
  • Access to pika-config.ts for configuration
  • Understanding of your agents and tools
  • Familiarity with AWS CDK or Serverless Framework (for IaC)
  • Entity feature configured (optional, for entity-based directives)

Instruction Augmentation dynamically enhances agent prompts by injecting relevant semantic directives based on context. The system:

  1. Identifies context from chat app, agent, tool, and user session
  2. Queries relevant directives based on scope matching
  3. LLM selects directives that are relevant to the user's query
  4. Injects instructions into the agent prompt before LLM invocation
  • Contextual Intelligence: Instructions adapt to specific contexts
  • Personalization: Different instructions for different users/entities
  • Maintainability: Update instructions without changing agent code
  • Scalability: Add new contexts without redeploying agents
  • Granular Control: Target instructions at multiple levels

Configure instruction augmentation in your pika-config.ts.

Location: apps/pika-chat/pika-config.ts

export const pikaConfig: PikaConfig = {
siteFeatures: {
instructionAugmentation: {
enabled: true
}
}
};

Semantic directives use scopes to determine when they're relevant.

Simple Scopes:

'chatapp#ecommerce-store' // Chat app specific
'agent#customer-service-bot' // Agent specific
'tool#weather-forecast-api' // Tool specific
'entity#acme-corp-123' // Entity specific

Compound Scopes:

Currently supported: Agent + Entity

'agent#sales-assistant#entity#enterprise-customer-123'

When creating directives, specify scopeType and scopeValue:

{
scopeType: 'chatapp',
scopeValue: 'ecommerce-store',
// System constructs: scope = 'chatapp#ecommerce-store'
}
{
scopeType: 'agent-entity',
scopeValue: {
agent: 'support-bot',
entity: 'premium-customer-456'
},
// System constructs: scope = 'agent#support-bot#entity#premium-customer-456'
}

Create directives using the admin API:

const response = await fetch('/api/chat-admin/semantic-directive', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
semanticDirective: {
scopeType: 'chatapp',
scopeValue: 'weather-app',
id: 'severe-weather-alerts',
description: 'Safety instructions for severe weather conditions',
instructions: 'When reporting severe weather, always emphasize safety precautions and recommend checking local authorities for emergency guidance.',
createdBy: 'admin-user',
lastUpdatedBy: 'admin-user'
},
userId: 'admin-user'
})
});

Option B: Via Infrastructure as Code (CDK)

Section titled “Option B: Via Infrastructure as Code (CDK)”

Define directives in your CDK stack:

import { SemanticDirectiveForCreateOrUpdate } from 'pika-shared/types/chatbot/chatbot-types';
import { gzipAndBase64EncodeString } from 'pika-shared/util/server-utils';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as cdk from 'aws-cdk-lib';
// Get the custom resource ARN from SSM
const semanticDirectiveCustomResourceArn = ssm.StringParameter.valueForStringParameter(
this,
`/stack/${pikaServiceProjNameKebabCase}/${stage}/lambda/semantic_directive_custom_resource_arn`
);
// Define your semantic directives
const semanticDirectiveData = {
userId: `cloudformation/${this.stackName}`,
semanticDirectives: [
{
scopeType: 'chatapp',
scopeValue: 'my-app',
id: 'welcome-message',
description: 'Friendly welcome for new users',
instructions: 'Start conversations with a warm welcome and brief explanation of your capabilities.',
createdBy: `cloudformation/${this.stackName}`,
lastUpdatedBy: `cloudformation/${this.stackName}`
},
{
scopeType: 'agent-entity',
scopeValue: {
agent: 'my-agent',
entity: 'enterprise-corp'
},
id: 'enterprise-features',
description: 'Enterprise-specific features for enterprise-corp',
instructions: 'Highlight enterprise features like dedicated support, advanced analytics, custom integrations, and SLA guarantees.',
createdBy: `cloudformation/${this.stackName}`,
lastUpdatedBy: `cloudformation/${this.stackName}`
}
]
};
// Create the custom resource
new cdk.CustomResource(this, 'MySemanticDirectives', {
serviceToken: semanticDirectiveCustomResourceArn,
properties: {
Stage: stage,
SemanticDirectiveData: gzipAndBase64EncodeString(
JSON.stringify(semanticDirectiveData)
),
Timestamp: String(Date.now())
}
});

Define directives in serverless.yml:

custom:
pika:
semanticDirectiveCustomResourceArn: ${ssm:/stack/my-pika-service/dev/lambda/semantic_directive_custom_resource_arn}
semanticDirectives:
- userId: 'serverless-stack'
semanticDirectives:
- scopeType: 'chatapp'
scopeValue: 'serverless-app'
id: 'error-handling'
description: 'Guidelines for handling system errors gracefully'
instructions: 'When system errors occur, apologize, explain the issue briefly, and offer alternative solutions or suggest trying again later.'
createdBy: 'serverless-stack'
lastUpdatedBy: 'serverless-stack'
- scopeType: 'tool'
scopeValue: 'payment-processor'
id: 'payment-security'
description: 'Security reminders for payment processing'
instructions: 'Always remind users that we never store credit card information and all payments are processed securely through our encrypted payment gateway.'
createdBy: 'serverless-stack'
lastUpdatedBy: 'serverless-stack'

Step 4: Integrate with Entity Feature (Optional)

Section titled “Step 4: Integrate with Entity Feature (Optional)”

Enable entity-based semantic directives for personalized experiences.

Location: apps/pika-chat/pika-config.ts

siteFeatures: {
entity: {
enabled: true,
attributeName: 'accountId', // Field in user.customData
displayNameSingular: 'Account',
displayNamePlural: 'Accounts'
},
instructionAugmentation: {
enabled: true
}
}

Your authentication provider sets the entity identifier:

export default class YourAuthProvider extends AuthProvider {
private createAuthenticatedUser(userData: any): AuthenticatedUser {
return {
userId: userData.id,
// ... other fields
customData: {
accountId: 'enterprise-customer-123', // Entity identifier
email: userData.email
}
};
}
}
// Direct entity scope
{
scopeType: 'entity',
scopeValue: 'enterprise-customer-123',
id: 'company-policies',
description: 'Company-specific policies and procedures',
instructions: 'Always reference Enterprise Customer 123 policies when discussing procedures.'
}
// Compound agent + entity scope
{
scopeType: 'agent-entity',
scopeValue: {
agent: 'support-bot',
entity: 'enterprise-customer-123'
},
id: 'escalation-process',
description: 'Customer-specific escalation procedures',
instructions: 'For Enterprise Customer 123, escalate to their dedicated account manager for priority issues.'
}
const response = await fetch('/api/chat-admin/semantic-directive/search', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
scopes: ['chatapp#weather-app'],
includeInstructions: true,
limit: 50
})
});
const { semanticDirectives, nextToken } = await response.json();
await fetch('/api/chat-admin/semantic-directive', {
method: 'POST',
body: JSON.stringify({
semanticDirective: {
...existingDirective,
instructions: 'Updated instructions...',
lastUpdatedBy: 'admin-user'
},
userId: 'admin-user'
})
});
await fetch('/api/chat-admin/semantic-directive', {
method: 'DELETE',
body: JSON.stringify({
semanticDirective: {
scope: 'chatapp#weather-app',
id: 'severe-weather-alerts'
},
userId: 'admin-user'
})
});
interface SemanticDirective {
scopeType: 'chatapp' | 'agent' | 'tool' | 'entity' | 'agent-entity';
scopeValue: string | number | { agent: string; entity: string };
id: string; // Unique within scope
description: string; // For LLM selection
instructions: string; // Injected into prompt
groupId?: string; // For IaC grouping
createdBy: string;
lastUpdatedBy: string;
createDate: string; // ISO 8601
lastUpdate: string; // ISO 8601
}

Verify instruction augmentation works correctly:

{
scopeType: 'chatapp',
scopeValue: 'ecommerce-store',
id: 'return-policy',
description: 'Return policy details for customer inquiries',
instructions: 'Our return policy allows returns within 30 days for standard customers and 60 days for premium members. All items must be in original condition.'
}
{
scopeType: 'tool',
scopeValue: 'payment-processor',
id: 'payment-errors',
description: 'How to handle payment processing errors',
instructions: 'If payment fails, suggest checking card details, trying a different payment method, or contacting their bank. Never ask for card details in chat.'
}
{
scopeType: 'entity',
scopeValue: 'premium-customer-gold',
id: 'premium-perks',
description: 'Premium customer benefits and features',
instructions: 'Remind premium customers they have: free expedited shipping, priority support, exclusive early access to sales, and 90-day returns.'
}
{
scopeType: 'agent-entity',
scopeValue: {
agent: 'sales-assistant',
entity: 'enterprise-account-456'
},
id: 'enterprise-pricing',
description: 'Enterprise-specific pricing and discounts',
instructions: 'This enterprise account has volume discounts: 20% off orders over $10k, 30% off over $50k, and dedicated account manager for custom quotes.'
}
  • Single Responsibility: Each directive addresses one specific scenario
  • Clear Descriptions: Help LLM make accurate selection decisions
  • Actionable Instructions: Provide specific, actionable guidance
  • Regular Review: Periodically review and update for relevance
  • Consistent Naming: Use clear, consistent naming conventions
  • Hierarchical Organization: Structure from general to specific
  • Documentation: Document scope meanings and usage
  • Namespace Separation: Use distinct prefixes for different domains
  • Scope Targeting: Use specific scopes to minimize candidates
  • Instruction Brevity: Keep instructions concise but complete
  • Monitor Selection: Track directive selection performance
  • Cache Appropriately: Implement caching where beneficial
  • Version Control: Track directive changes over time
  • A/B Testing: Test directive variations
  • User Feedback: Collect feedback on directive-enhanced responses
  • Continuous Improvement: Iteratively refine based on usage
  • Verify feature enabled in pika-config.ts
  • Check scope patterns match current context
  • Review directive descriptions for clarity
  • Ensure LLM selection service is working
  • Check CloudWatch logs for selection process
  • Verify entity feature enabled
  • Check entity.attributeName matches user customData field
  • Confirm user has entity value populated
  • Ensure entity value matches directive scopeValue
  • Review authentication provider logic
  • Reduce number of candidate directives per scope
  • Optimize directive descriptions for faster selection
  • Implement caching for frequently used directives
  • Monitor LLM selection latency
  • Consider batch processing for similar queries
  • Verify admin permissions for API access
  • Check directive structure matches interface
  • Ensure scope is constructed correctly from scopeType and scopeValue
  • Review DynamoDB table for directive storage
  • Check CloudWatch logs for API errors