Skip to main content

Context & AI Utilities

Format memories for LLM context injection and monitor memories in real-time.

Overview

The CLI provides utilities for AI agents and automation workflows. The context command retrieves memories formatted for LLM prompt injection, while watch monitors memories in real-time.

context Command

Get memories formatted for injection into LLM prompts. Designed for AI agents and automation.

Basic Usage

# Get context for a query
trix context "user preferences"

# Limit to token budget
trix context "project info" --max-tokens 2000

# Filter by space
trix context "api docs" --space backend

# Limit number of memories
trix context "recent notes" --limit 5

# Set minimum similarity score
trix context "api docs" --min-score 0.5

Output Formats

The context command supports multiple output formats for different use cases:

FormatDescriptionUse Case
plainRaw content, newline-separatedDefault, simple injection (default)
xmlXML-tagged formatStructured parsing, Claude XML tags
numberedNumbered list with IDsReference tracking, citations
markdownMarkdown with headersHuman-readable documentation
claudeClaude-optimized XML tagsPrompt injection for Anthropic models
openaiOpenAI message-array formatSystem/context blocks for GPT prompts
jsonStructured JSON payloadProgrammatic consumption
# Plain format (default)
trix context "api docs" --format plain

# XML format for structured parsing
trix context "api docs" --format xml

# Numbered for reference tracking
trix context "recent notes" --format numbered

# Markdown for documentation
trix context "project info" --format markdown

Token Management

Control output size with token budgets to stay within LLM context limits:

# Limit to ~2000 tokens (approximately 8000 characters)
trix context "project context" --max-tokens 2000

# Combine with memory limit
trix context "api docs" --max-tokens 4000 --limit 10

# Token estimation uses ~4 chars per token
# Actual tokens may vary by LLM tokenizer

Token estimation uses approximately 4 characters per token. Actual token counts may vary depending on the LLM tokenizer (GPT, Claude, etc.).

Scripting with Context

# Use in bash scripts
CONTEXT=$(trix context "user preferences" --max-tokens 1000)
echo "Based on this context: $CONTEXT"

# Pipe to LLM CLI
trix context "api docs" | llm "Summarize these API docs"

# Combine with other tools
trix context "recent errors" --format xml > context.xml

# Silent operation (no spinner, returns empty for no results)
PREFS=$(trix context "preferences" --max-tokens 500)
if [ -n "$PREFS" ]; then
  echo "Found preferences: $PREFS"
fi

The context command is designed for silent, agent-friendly operation. It doesn't show spinners and returns empty output (not an error) when no memories are found.

context Options

OptionDescriptionDefault
--max-tokensMaximum approximate tokens (0 = unlimited)0
--format, -fOutput format: plain, xml, numbered, markdown, claude, openai, jsonplain
--space, -sSearch within specific spacedefault
--limit, -lMaximum memories to include10
--min-scoreMinimum similarity score (0.0-1.0)0.3
--with-graphInclude relationship graph summary between retrieved memories (use --with-graph=false to disable)true

Relationship Graph

Include a summary of relationships between retrieved memories with the --with-graph flag. This helps AI agents understand how memories connect to each other.

# Include relationship graph
trix context "architecture decisions" --with-graph

# With XML format for structured parsing
trix context "api docs" --format xml --with-graph

# Limit results but show their connections
trix context "project info" --limit 5 --with-graph

The relationship graph summary shows connections like supports, references, derived_from, and contradicts between the retrieved memories. This context helps AI agents understand the knowledge structure and make more informed responses.

watch Command

Monitor memories in real-time as they're created. Useful for live dashboards, monitoring, and event-driven workflows.

Basic Usage

# Watch all new memories
trix watch

# Watch specific space
trix watch --space work

# Watch for tagged memories
trix watch --tags alert,important

# Watch with custom interval
trix watch --interval 10s

# Press Ctrl+C to stop watching

Output Formats

FormatDescription
briefCompact single-line format (default)
fullComplete memory details
jsonJSON format for piping to other tools
# Brief format (default)
trix watch --format brief

# Full details
trix watch --format full

# JSON for piping
trix watch --format json | jq '.content'

Filtering

# Watch specific space only
trix watch --space alerts

# Watch for specific tags
trix watch --tags error,critical

# Combine filters
trix watch --space production --tags error --interval 3s

# Minimum interval is 1 second
trix watch --interval 1s

Use Cases

  • Live Dashboard: Display new memories on a screen or dashboard
  • Alerting: Watch for memories tagged "alert" or "error"
  • Event-Driven: Trigger actions when new memories appear
  • Monitoring: Track activity in specific spaces or projects
  • Integration: Pipe to other tools for processing
# Pipe to notification system
trix watch --tags alert --format json | \
  jq -r '.content' | \
  xargs -I {} notify-send "Trix Alert" "{}"

# Log to file
trix watch --space production > production.log

# Trigger actions
trix watch --tags deploy --format json | \
  while read -r line; do
    echo "$line" | jq '.content' | bash
  done

The watch command polls the API at regular intervals. Be mindful of polling frequency to avoid excessive API calls. The minimum interval is 1 second.

watch Options

OptionDescriptionDefault
--space, -sWatch specific spaceall spaces
--tags, -tFilter by tags (comma-separated)all tags
--interval, -iPolling interval (min: 1s)5s
--format, -fOutput format: brief, full, jsonbrief

Integration Examples

AI Agent Workflow

#!/bin/bash
# Example: AI agent with Trix context

# Get relevant context for user query
USER_QUERY="How do I authenticate?"
CONTEXT=$(trix context "$USER_QUERY" --max-tokens 2000 --format xml)

# Inject into LLM prompt
PROMPT="<context>$CONTEXT</context>

<query>$USER_QUERY</query>

Answer the user's query using the provided context."

# Send to LLM (example with llm CLI)
echo "$PROMPT" | llm

Event-Driven Automation

#!/bin/bash
# Example: React to new memories with specific tags

trix watch --tags deploy --format json | while read -r memory; do
  CONTENT=$(echo "$memory" | jq -r '.content')
  ID=$(echo "$memory" | jq -r '.id')

  echo "New deployment memory detected: $ID"
  echo "Content: $CONTENT"

  # Trigger deployment pipeline
  ./deploy.sh "$CONTENT"

  # Update memory with result
  trix mem update "$ID" --tags "deploy,processed"
done

Context Caching for Performance

#!/bin/bash
# Example: Cache context to reduce API calls

CACHE_FILE="/tmp/trix-context.cache"
CACHE_TTL=300  # 5 minutes

# Check if cache exists and is fresh
if [ -f "$CACHE_FILE" ] && \
   [ $(($(date +%s) - $(stat -c %Y "$CACHE_FILE"))) -lt $CACHE_TTL ]; then
  CONTEXT=$(cat "$CACHE_FILE")
else
  # Refresh cache
  CONTEXT=$(trix context "project knowledge" --max-tokens 4000)
  echo "$CONTEXT" > "$CACHE_FILE"
fi

# Use cached context
echo "Using context: $CONTEXT"

Next Steps