Cognition Layer

The Cognition Layer provides reasoning capabilities with explainable thought processes and pattern recognition.

cognition.reason()

Perform multi-step reasoning with context and capture the thought process.

Signature

typescript
reason(params: ReasonParams): Promise<ReasonResponse>

Parameters

NameTypeRequiredDescription
querystringYesThe query or question
contextstring[]NoMemory IDs for context
reflectionbooleanNoEnable reasoning capture
modelstringNoLLM model to use
temperaturenumberNo0-1 (default: 0.7)
systemPromptstringNoCustom system prompt

Returns

typescript
{
  answer: string;         // The final answer
  reasoning: string[];    // Thought process steps
  confidence: number;     // 0-1 confidence score
  sources: string[];      // Context sources used
  model: string;          // Model that generated response
  tokensUsed: number;     // Total tokens consumed
}

Example

typescript
const result = await client.cognition.reason({
  query: "What are the best practices for AI cost optimization?",
  reflection: true,
  temperature: 0.7,
  model: "gpt-4"
});

console.log('Answer:', result.answer);
console.log('Reasoning:', result.reasoning);
console.log('Confidence:', result.confidence);

cognition.reflect()

Analyze past reasoning and extract patterns for improvement.

Signature

typescript
reflect(params: ReflectParams): Promise<ReflectResponse>

Example

typescript
const reflection = await client.cognition.reflect({
  sessionId: 'session_123',
  analysisDepth: 'deep'
});

console.log('Patterns:', reflection.patterns);
console.log('Insights:', reflection.insights);
console.log('Improvements:', reflection.suggestions);

Advanced Usage

Multi-Step Reasoning

typescript
// Step 1: Initial reasoning
const step1 = await client.cognition.reason({
  query: "Analyze Q4 sales data",
  reflection: true
});

// Step 2: Deep dive based on findings
const step2 = await client.cognition.reason({
  query: `Based on these findings: ${step1.answer}, 
         recommend Q1 strategies`,
  context: [step1.sources],
  reflection: true
});

console.log('Final Recommendation:', step2.answer);
console.log('Complete Reasoning Chain:', [
  ...step1.reasoning,
  ...step2.reasoning
]);

With Memory Context

typescript
// Store relevant knowledge
const docs = await Promise.all([
  client.memory.store({ content: "Doc 1..." }),
  client.memory.store({ content: "Doc 2..." }),
  client.memory.store({ content: "Doc 3..." })
]);

// Reason with full context
const result = await client.cognition.reason({
  query: "Summarize key insights",
  context: docs.map(d => d.id),
  reflection: true
});

Best Practices

  • • Enable reflection for explainable AI and compliance
  • • Use specific queries for better reasoning quality
  • • Provide relevant context from Memory Layer
  • • Set appropriate temperature (lower = more focused)
  • • Monitor token usage for cost optimization
  • • Cache frequently asked questions