Performance and Best Practices
Overview
This guide covers best practices for building efficient, maintainable Live Pages applications.
Performance Optimization
1. Use pt.get() for Single Entities
When you know the entity ID, always use pt.get() for the fastest retrieval:
2. Implement Server-Side Filtering
Always filter on the server rather than loading all data and filtering client-side:
3. Use Appropriate Operators
Choose the most efficient operator for your use case:
4. Cache Static Data
Cache data that doesn't change frequently:
5. Use Pagination
For large datasets, always implement pagination:
6. Batch Operations
Use Promise.all() for parallel operations:
7. Provide Immediate Feedback with Processing Status
For file uploads or long-running operations, create database rows immediately with a PROCESSING status to provide instant user feedback, then update them when processing completes.
Pattern: Add-Then-Update
When to Use Each Approach
Upload-Then-Add (chatdb_add):
Goals and automation (chat, email)
Background tasks
No user waiting for feedback
Simpler with fewer failure modes
Add-Then-Update (pt.add + chatdb_edit):
Interactive Live Page uploads
User is actively waiting
Immediate feedback is critical
Dashboard/real-time applications
Comparison:
Aspect | Upload-Then-Add | Add-Then-Update |
|---|---|---|
User Feedback | Delayed | Immediate |
Operations | 1 (add only) | 2 (add + edit) |
Complexity | Simple | More complex |
Orphaned Rows | None | Possible |
Best For | Automation | Interactive UIs |
Best Practices for Add-Then-Update
1. Handle Cleanup on Failure:
2. Use Clear Status Values:
3. Add Auto-Refresh:
4. Show Processing Indicators:
8. Implement Debouncing
Debounce search inputs to reduce API calls:
9. Cache Page Results
Implement caching for pagination:
Error Handling
1. Always Handle Errors
Wrap data operations in try-catch blocks:
2. Provide User Feedback
Show meaningful error messages to users:
3. Implement Fallback Strategies
Provide fallbacks when operations fail:
Code Organization
1. Separate Concerns
Organize code into logical functions:
2. Use Meaningful Names
3. Create Reusable Components
Data Management Best Practices
1. Always Merge When Editing
2. Validate Before Saving
3. Handle Missing Members Gracefully
4. Use Appropriate Limits
Using Goals with Live Pages
What are Goals?
Goals are automatic AI instructions that execute when specific conditions are met in a chat. When combined with Live Pages, Goals enable powerful automation workflows where data can be processed and stored in your database without manual intervention.
How Goals Work with Live Pages
When you set up a Goal in your chat settings, the AI automatically:
Detects when the goal condition is triggered (e.g., file upload, specific keywords)
Executes the instructions you've defined in the Goal
Can use chatdb tools to create/update/query database entities
Stores results that your Live Page can display and interact with
This creates a seamless integration between:
Direct chat interactions (uploading files, sending messages)
Email forwarding (files sent to chat email address)
API uploads (files uploaded programmatically via PrimeThink API)
Chat mentions (files uploaded when the chat is mentioned in other conversations)
AI processing (extraction, categorization, validation)
Database storage (structured data in entities)
Live Page display (visualization and interaction)
Common Use Cases for Goals with Live Pages
1. Document Processing
2. Email Automation
3. Data Entry Shortcuts
4. File Analysis
5. Content Categorization
6. API Integration
7. Chat Mention Processing
8. Multi-Channel Document Inbox
Best Practices for Writing Goal Prompts
1. Be Explicit About Tool Usage
Always specify which chatdb tool to use:
2. Specify Entity Structure Clearly
Define exactly what fields to create:
3. Handle Edge Cases
Account for scenarios where data might not be available:
4. Request Structured Responses
Ask for JSON responses for easier validation:
5. Create One Record Per Item
Be explicit about quantity:
6. Maintain Consistency
Use the same entity names and data structures across goals:
Goal Example for Live Pages
Here's a complete example of a Goal that works with a Live Page:
Goal Trigger: If a user uploads a PDF or DOCX file
Goal Instructions:
Corresponding Live Page:
Displays all documents using
pt.list({ entityNames: ['document'] })Shows category, summary, processing status
Allows filtering by category or status
Provides "View" button to see document text with
pt.getDocumentText()Shows "Re-process" button for pending items using
pt.addMessage()
Benefits of Goals with Live Pages
Unified Experience:
Users can upload files via Live Page, chat message, email, API, or chat mentions
All uploads are processed consistently regardless of source
All results appear in the same Live Page interface
Single Goal handles all four upload channels
Automation:
No manual data entry required
AI handles extraction and categorization
Reduces human error
Zero-touch processing for API and automated uploads
Flexibility:
Live Page: Visual interface for interactive uploads
Chat: Conversational interface for manual uploads
Email: Integration with existing email workflows
API: Programmatic uploads for system integrations
Chat Mentions: Context-aware processing when the chat is mentioned in other conversations
Scalability:
Process single files or batch uploads
Same Goal handles all sources (chat, email, API, chat mentions)
Live Page adapts to any data volume
Supports high-throughput API integrations
Testing Goals with Live Pages
1. Test with Live Page Upload First:
Use
pt.uploadFiles()with instructionsVerify AI creates correct database entities
Check that Live Page displays data correctly
2. Test Chat Upload:
Upload file directly in chat message
Verify Goal triggers and runs
Confirm same entity structure is created
3. Test Email Upload:
Forward email with attachment to chat email address
Verify Goal triggers automatically
Confirm consistent entity structure
4. Test API Upload:
Upload file programmatically via PrimeThink API
Verify Goal triggers for API uploads
Confirm API uploads create same entity structure
5. Test Chat Mention Upload:
Mention the chat in another conversation with file attachment
Verify Goal triggers when chat is mentioned
Confirm entity structure matches other channels
6. Test Edge Cases:
Upload file without text
Upload unsupported format
Upload very large file
Upload multiple files at once
Test each channel with edge cases
7. Verify Multi-Channel Consistency:
Compare entities from all four channels (Live Page, chat, email, API, chat mentions)
Ensure entity_name matches exactly across all sources
Confirm data structure is identical regardless of upload method
Verify all uploads appear correctly in Live Page
Goal + Live Page Checklist
[ ] Goal uses explicit chatdb tool names
[ ] Entity structure is clearly defined
[ ] Edge cases are handled (no text, errors)
[ ] Response format is specified (JSON recommended)
[ ] Entity names are consistent with Live Page queries
[ ] Data field names match what Live Page expects
[ ] Status values are well-defined and consistent
[ ] Goal tested with Live Page uploads
[ ] Goal tested with chat uploads
[ ] Goal tested with email uploads
[ ] Goal tested with API uploads
[ ] Goal tested with chat mention uploads
[ ] Live Page can display all possible status values
[ ] Error cases have retry mechanisms
[ ] All channels create identical entity structures
Security Best Practices
1. Escape User Input
Always escape HTML when displaying user-generated content:
2. Validate Input Length
3. Confirm Destructive Actions
UI/UX Best Practices
1. Show Loading States
2. Provide Visual Feedback
3. Handle Empty States
Troubleshooting
Common Issues
Issue: pt.list() returns empty array
Solutions:
Check that entity name is correct
Verify filters are valid
Try without filters to see if entities exist
Check browser console for errors
Issue: Data not updating after edit
Solutions:
Ensure you're merging with existing data
Check that you're calling loadTasks() after edit
Verify the edit was successful
Issue: Performance is slow
Solutions:
Implement pagination
Use server-side filtering
Cache static data
Reduce data transfer with appropriate limits
Testing Tips
1. Test with Empty Data
2. Test with Large Datasets
3. Test Error Scenarios
Performance Checklist
[ ] Use
pt.get()for single entity lookups[ ] Implement server-side filtering
[ ] Use pagination for large datasets
[ ] Cache chat members and other static data
[ ] Implement debouncing for search inputs
[ ] Use batch operations with
Promise.all()[ ] Provide immediate feedback with PROCESSING status for uploads
[ ] Choose appropriate filter operators
[ ] Set reasonable limits on queries
[ ] Implement caching where appropriate
[ ] Show loading states
[ ] Handle errors gracefully
[ ] Validate user input
[ ] Escape HTML content
[ ] Test with empty and large datasets
[ ] Use Goals for unified upload experience (Live Page + Chat + Email)
[ ] Ensure Goal entity structure matches Live Page queries
[ ] Make Goal prompts explicit about chatdb tool usage
[ ] Choose appropriate pattern: Upload-Then-Add for automation, Add-Then-Update for interactive UIs
Next Steps
Creating Live Pages - Back to main guide
Data Management API Reference - Learn about all pt API methods
Filtering and Querying - Advanced filtering techniques
Complete Examples - See best practices in action