# Class ContextManager

Manages context window for agents with token budget enforcement.

- Defined in packages/nexus-agents/src/agents/context-manager.ts:58

##### Index

### Constructors

constructor

### Methods

add
remove
get
canAdd
getByCategory
getAllItems
buildMessages
getSystemPrompt
getStats
getRemainingTokens
getTotalRemainingTokens
clear
clearCategory
countTokens

## Constructors

### constructor

- new ContextManager(config: ContextManagerConfig): ContextManager

  #### Parameters

  - config: ContextManagerConfig

  #### Returns ContextManager

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:71

## Methods

### add

- add(
  item: Omit<ContextItem, "tokenCount" | "addedAt">,
  ): Promise<Result<ContextItem, ValidationError>>

  Add an item to the context. Returns Result with the added item or error.

  #### Parameters

  - item: Omit<ContextItem, "tokenCount" | "addedAt">

  #### Returns Promise<Result<ContextItem, ValidationError>>

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:90

### remove

- remove(id: string): boolean

  Remove an item from the context. Returns true if removed, false if not found.

  #### Parameters

  - id: string

  #### Returns boolean

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:200

### get

- get(id: string): ContextItem | undefined

  Get an item by ID.

  #### Parameters

  - id: string

  #### Returns ContextItem | undefined

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:216

### canAdd

- canAdd(
  content: string,
  category: "system" | "task" | "active",
  ): Promise<boolean>

  Check if an item with the given content can be added to a category.

  #### Parameters

  - content: string
  - category: "system" | "task" | "active"

  #### Returns Promise<boolean>

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:221

### getByCategory

- getByCategory(category: "system" | "task" | "active"): ContextItem[]

  Get all items in a category, sorted by priority (desc) then addedAt (asc).

  #### Parameters

  - category: "system" | "task" | "active"

  #### Returns ContextItem[]

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:237

### getAllItems

- getAllItems(): ContextItem[]

  Get all items sorted by priority (desc) then addedAt (asc).

  #### Returns ContextItem[]

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:242

### buildMessages

- buildMessages(): Message[]

  Build messages array from context items for model requests.

  #### Returns Message[]

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:247

### getSystemPrompt

- getSystemPrompt(): string | undefined

  Get the system prompt from system category items.

  #### Returns string | undefined

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:252

### getStats

- getStats(): ContextStats

  Get current context statistics.

  #### Returns ContextStats

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:257

### getRemainingTokens

- getRemainingTokens(category: "system" | "task" | "active"): number

  Get remaining tokens available in a category.

  #### Parameters

  - category: "system" | "task" | "active"

  #### Returns number

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:267

### getTotalRemainingTokens

- getTotalRemainingTokens(): number

  Get total remaining tokens across all categories.

  #### Returns number

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:274

### clear

- clear(): void

  Clear all items from the context.

  #### Returns void

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:280

### clearCategory

- clearCategory(category: "system" | "task" | "active"): number

  Clear items from a specific category. Returns number of items removed.

  #### Parameters

  - category: "system" | "task" | "active"

  #### Returns number

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:288

### countTokens

- countTokens(text: string): Promise<number>

  Count tokens in text using adapter or fallback estimation.

  #### Parameters

  - text: string

  #### Returns Promise<number>

  - Defined in packages/nexus-agents/src/agents/context-manager.ts:308