> ## Documentation Index
> Fetch the complete documentation index at: https://ricardovelit.com/axon-docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Context

> Configure execution environments with memory, depth, and model parameters

## Overview

A **context** defines the execution environment for a flow—memory scope, reasoning depth, model parameters, and language preferences. While personas define "who" executes a flow, contexts define "how" it's executed.

## Syntax

```axon theme={null}
context <Name> {
  memory: <scope>
  language: <string>
  depth: <level>
  max_tokens: <integer>
  temperature: <float>
  cite_sources: <boolean>
}
```

## Fields

### `memory` (optional)

**Type:** One of `session`, `persistent`, `none`, `ephemeral`

Defines the memory scope for the execution.

```axon theme={null}
context LongTermContext {
  memory: persistent    // Memory persists across sessions
}

context OneOffTask {
  memory: none         // No memory retention
}

context ChatSession {
  memory: session      // Memory lasts for the session
}

context TemporaryWork {
  memory: ephemeral    // Short-lived memory
}
```

**Memory Scopes:**

| Scope        | Lifetime                     | Use Case                              |
| ------------ | ---------------------------- | ------------------------------------- |
| `none`       | No memory                    | Stateless operations, one-off queries |
| `ephemeral`  | Single flow execution        | Temporary working memory              |
| `session`    | Current session/conversation | Chat contexts, iterative tasks        |
| `persistent` | Across sessions              | Long-term knowledge, user preferences |

### `language` (optional)

**Type:** `String` (ISO language code)

Preferred language for the execution context.

```axon theme={null}
context EnglishContext {
  language: "en"
}

context SpanishReview {
  language: "es"
  depth: exhaustive
}
```

### `depth` (optional)

**Type:** One of `shallow`, `standard`, `deep`, `exhaustive`

Defines the reasoning depth and thoroughness of analysis.

```axon theme={null}
context QuickScan {
  depth: shallow       // Fast, surface-level
}

context StandardReview {
  depth: standard      // Balanced depth
}

context DetailedAnalysis {
  depth: deep          // Thorough investigation
}

context ComprehensiveAudit {
  depth: exhaustive    // Maximum detail
}
```

**Depth Levels:**

| Level        | Characteristics             | Typical Use                     |
| ------------ | --------------------------- | ------------------------------- |
| `shallow`    | Fast, high-level overview   | Quick summaries, previews       |
| `standard`   | Balanced depth and speed    | General purpose tasks           |
| `deep`       | Thorough, detailed analysis | Complex reasoning, research     |
| `exhaustive` | Maximum detail, multi-pass  | Legal review, critical analysis |

### `max_tokens` (optional)

**Type:** `Integer` (must be positive)

Maximum number of tokens for the model's response.

```axon theme={null}
context BriefResponses {
  max_tokens: 500      // Short responses
}

context StandardContext {
  max_tokens: 4096     // Typical context window
}

context ExtendedReport {
  max_tokens: 16000    // Long-form content
}
```

**Guidelines:**

* **500-1000**: Brief summaries, quick responses
* **2000-4096**: Standard tasks, moderate complexity
* **8000-16000**: Long-form reports, comprehensive analysis
* **16000+**: Extended documents, multi-part responses

### `temperature` (optional)

**Type:** `Float` (range: `0.0` to `2.0`)

Controls randomness/creativity in model outputs. Lower = more deterministic, higher = more creative.

```axon theme={null}
context FactualAnalysis {
  temperature: 0.0     // Deterministic, factual
}

context LegalReview {
  temperature: 0.3     // Mostly deterministic, slight variation
}

context BalancedGeneration {
  temperature: 0.7     // Balanced creativity
}

context CreativeWriting {
  temperature: 1.2     // High creativity
}
```

**Temperature Guidelines:**

| Range       | Behavior             | Use Cases                         |
| ----------- | -------------------- | --------------------------------- |
| `0.0 - 0.3` | Highly deterministic | Facts, analysis, legal, medical   |
| `0.4 - 0.7` | Balanced             | General purpose, Q\&A             |
| `0.8 - 1.2` | Creative             | Brainstorming, content generation |
| `1.3 - 2.0` | Highly creative      | Experimental, artistic            |

### `cite_sources` (optional)

**Type:** `Boolean` (default: `false`)

Whether to require source citations in this context.

```axon theme={null}
context ResearchMode {
  cite_sources: true
  depth: deep
}

context CasualChat {
  cite_sources: false
}
```

## Complete Examples

### Legal Review Context

```axon theme={null}
context LegalReview {
  memory: session
  language: "en"
  depth: exhaustive
  max_tokens: 4096
  temperature: 0.3
  cite_sources: true
}
```

**Use Case:** Comprehensive legal document analysis requiring accuracy, source tracking, and thorough investigation.

### Quick Summary Context

```axon theme={null}
context QuickSummary {
  memory: none
  depth: shallow
  max_tokens: 500
  temperature: 0.5
}
```

**Use Case:** Fast, stateless summarization of documents.

### Research Context

```axon theme={null}
context AcademicResearch {
  memory: persistent
  language: "en"
  depth: deep
  max_tokens: 8000
  temperature: 0.4
  cite_sources: true
}
```

**Use Case:** Long-term research projects with citation requirements and detailed analysis.

### Creative Writing Context

```axon theme={null}
context CreativeMode {
  memory: session
  depth: standard
  max_tokens: 4096
  temperature: 1.0
  cite_sources: false
}
```

**Use Case:** Creative content generation with higher variability.

### Production Context

```axon theme={null}
context Production {
  memory: session
  language: "en"
  depth: standard
  max_tokens: 4096
  temperature: 0.5
}
```

**Use Case:** Balanced production environment for general tasks.

## Usage with Run Statements

Contexts are activated using the `within` modifier:

```axon theme={null}
context AnalysisContext {
  depth: deep
  temperature: 0.3
}

flow AnalyzeDocument(doc: Document) -> Report { ... }

run AnalyzeDocument(myDoc)
  as Analyst
  within AnalysisContext    // Execute in this context
```

## Best Practices

### 1. Match Context to Task Criticality

High-stakes tasks need stricter contexts:

```axon theme={null}
// High-stakes: legal, medical, financial
context CriticalAnalysis {
  depth: exhaustive
  temperature: 0.2
  cite_sources: true
}

// Low-stakes: brainstorming, drafts
context ExploratoryMode {
  depth: standard
  temperature: 0.8
  cite_sources: false
}
```

### 2. Memory Scope Tradeoffs

```axon theme={null}
// Persistent: for user-specific knowledge
context UserProfile {
  memory: persistent
  // Remember user preferences, history
}

// Session: for conversation context
context Conversation {
  memory: session
  // Remember within chat, forget after
}

// None: for stateless operations
context StatelessAPI {
  memory: none
  // No state, pure function
}
```

### 3. Temperature for Determinism

Lower temperature for consistency:

```axon theme={null}
// Need reproducibility? Use low temperature
context DataAnalysis {
  temperature: 0.1  // Nearly deterministic
  depth: deep
}

// Need variety? Use higher temperature
context ContentIdeas {
  temperature: 1.0  // More creative variation
}
```

### 4. Token Budgets

Set appropriate token limits:

```axon theme={null}
context APIEndpoint {
  max_tokens: 1000   // Keep responses concise for APIs
  temperature: 0.5
}

context ReportGeneration {
  max_tokens: 8000   // Allow detailed reports
  depth: deep
}
```

### 5. Combine with Personas

Contexts and personas work together:

```axon theme={null}
persona LegalExpert {
  domain: ["contract law"]
  tone: precise
}

context LegalReview {
  depth: exhaustive
  temperature: 0.3
  cite_sources: true
}

run AnalyzeContract(doc)
  as LegalExpert        // Who
  within LegalReview    // How
```

## Common Patterns

### Development vs. Production

```axon theme={null}
context Development {
  depth: shallow
  max_tokens: 1000
  temperature: 0.7
}

context Production {
  depth: deep
  max_tokens: 4096
  temperature: 0.5
  cite_sources: true
}

// Switch contexts based on environment
run MyFlow(data)
  as MyPersona
  within Production  // or Development
```

### Multi-Language Support

```axon theme={null}
context EnglishContext {
  language: "en"
  depth: standard
}

context SpanishContext {
  language: "es"
  depth: standard
}

context FrenchContext {
  language: "fr"
  depth: standard
}
```

### Tiered Analysis Depths

```axon theme={null}
context QuickPass {
  depth: shallow
  max_tokens: 1000
}

context StandardPass {
  depth: standard
  max_tokens: 4096
}

context DetailedPass {
  depth: deep
  max_tokens: 8000
}

context ComprehensivePass {
  depth: exhaustive
  max_tokens: 16000
  cite_sources: true
}

// Run multiple passes with increasing depth
run FirstPass(doc) within QuickPass
run SecondPass(doc) within DetailedPass
```

## Type Checking

The AXON type checker validates:

✅ **Memory scope**: Must be one of the valid scopes\
✅ **Depth level**: Must be one of the valid depth levels\
✅ **Temperature range**: Must be between 0.0 and 2.0\
✅ **Max tokens**: Must be positive\
✅ **Context references**: Referenced contexts must exist

```axon theme={null}
context Invalid {
  memory: unlimited           // ❌ Error: unknown memory scope
  depth: superficial          // ❌ Error: unknown depth level
  temperature: 3.0            // ❌ Error: out of range
  max_tokens: -100            // ❌ Error: must be positive
}

run MyFlow(data) within UnknownContext  // ❌ Error: undefined context
```

## Performance Considerations

### Depth vs. Speed

```axon theme={null}
// Fast but shallow
context QuickMode {
  depth: shallow
  max_tokens: 500
}

// Slow but thorough
context ThoroughMode {
  depth: exhaustive
  max_tokens: 16000
}
```

### Memory vs. Privacy

```axon theme={null}
// Better personalization, but stores data
context Personalized {
  memory: persistent
}

// Privacy-first, no retention
context Private {
  memory: none
}
```

## Related

* [Persona](/axon-docs/axon-docs/language/persona) — Define agent identities
* [Memory](/axon-docs/axon-docs/language/memory) — Configure semantic memory stores
* [Flow](/axon-docs/axon-docs/language/flow) — Build cognitive pipelines
* [Anchor](/axon-docs/axon-docs/language/anchor) — Set hard constraints
