# openaiotlab/contextagent architecture

> Architecture diagram of the GitHub repository openaiotlab/contextagent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openaiotlab/contextagent
- Repository: https://github.com/openaiotlab/contextagent

## Overview

ContextAgent is a context-aware proactive LLM agent evaluated through in-context learning (ICL) or supervised fine-tuning (SFT). In ICL, a user selects a model and live or sandbox mode; inference uses a tool registry, and results can be scored. The repository also includes LLaMA-Factory as the documented SFT implementation and a substantial training/inference framework. The README and source index support the main workflow and imports, but many tool implementations and training internals were not excerpted, so those relationships are intentionally limited.

Main entry points: [`LLaMA-Factory/src/llamafactory/chat`](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/chat) (Chat engines), [`src/icl/inference.py`](https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference.py) (Local ICL), [`src/icl/inference_api.py`](https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference_api.py) (API ICL), [`src/sft/inference.py`](https://github.com/openaiotlab/contextagent/blob/main/src/sft/inference.py) (SFT inference)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agent["Agent evaluation"]
  node_icl["Local ICL<br/>[inference.py]"]
  node_apiicl["API ICL<br/>[inference_api.py]"]
  node_sftinfer["SFT inference<br/>[inference.py]"]
  node_mode["Run mode<br/>[config.py]"]
  node_scores["Score results"]
  node_utils["Inference utilities<br/>[utils.py]"]
end

subgraph group_tools["Context tools"]
  node_registry["Tool registry<br/>[tool_registry.py]"]
  node_toolset["Context tools"]
end

subgraph group_factory["LLaMA-Factory"]
  node_factorytrain["Training workflows"]
  node_factorydata["Data processing"]
  node_factorychat["Chat engines"]
  node_factorymodel["Model loading"]
  node_quant["Quantization<br/>[quantization.py]"]
  node_factoryparams["Run parameters"]
  node_factoryapi["Inference API"]
  node_factoryweb["Web interface"]
  node_factoryextras["Runtime support"]
end

node_user(("Evaluator"))
node_services{{"External APIs"}}

node_user -->|"runs ICL"| node_icl
node_user -->|"runs API ICL"| node_apiicl
node_user -->|"evaluates SFT"| node_sftinfer
node_icl -->|"loads tools"| node_registry
node_apiicl -->|"loads tools"| node_registry
node_sftinfer -->|"loads tools"| node_registry
node_icl -->|"uses mode"| node_mode
node_apiicl -->|"uses mode"| node_mode
node_sftinfer -->|"uses mode"| node_mode
node_registry -->|"registers tools"| node_toolset
node_toolset -.->|"calls integrations"| node_services
node_user -->|"scores runs"| node_scores
node_icl -->|"uses utilities"| node_utils
node_apiicl -->|"uses utilities"| node_utils
node_sftinfer -->|"uses utilities"| node_utils
node_user -->|"launches SFT"| node_factorytrain
node_factorytrain -.->|"uses data pipeline"| node_factorydata
node_factorytrain -.->|"loads models"| node_factorymodel
node_factorychat -->|"uses templates"| node_factorydata
node_factorychat -->|"loads model"| node_factorymodel
node_factorychat -->|"configures quantization"| node_quant
node_factoryapi -->|"selects engine"| node_factorychat
node_factoryweb -->|"uses chat engine"| node_factorychat
node_factorychat -->|"accepts arguments"| node_factoryparams

click node_icl "https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference.py"
click node_apiicl "https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference_api.py"
click node_sftinfer "https://github.com/openaiotlab/contextagent/blob/main/src/sft/inference.py"
click node_mode "https://github.com/openaiotlab/contextagent/blob/main/src/config.py"
click node_registry "https://github.com/openaiotlab/contextagent/blob/main/src/tool_registry.py"
click node_toolset "https://github.com/openaiotlab/contextagent/tree/main/src/tools"
click node_scores "https://github.com/openaiotlab/contextagent/blob/main/src/calculate_scores.py"
click node_utils "https://github.com/openaiotlab/contextagent/blob/main/src/utils.py"
click node_factorytrain "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/train"
click node_factorydata "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/data"
click node_factorychat "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/chat"
click node_factorymodel "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/model"
click node_quant "https://github.com/openaiotlab/contextagent/blob/main/LLaMA-Factory/src/llamafactory/model/model_utils/quantization.py"
click node_factoryparams "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/hparams"
click node_factoryapi "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/api"
click node_factoryweb "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/webui"
click node_factoryextras "https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/extras"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_icl,node_apiicl,node_sftinfer,node_mode,node_scores,node_utils toneBlue
class node_registry,node_toolset toneAmber
class node_factorytrain,node_factorydata,node_factorychat,node_factorymodel,node_quant,node_factoryparams,node_factoryapi,node_factoryweb,node_factoryextras,node_services toneMint
class node_user toneIndigo
```

## Components

### Agent evaluation

- [Local ICL](https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference.py): `src/icl/inference.py`
- [API ICL](https://github.com/openaiotlab/contextagent/blob/main/src/icl/inference_api.py): `src/icl/inference_api.py`
- [SFT inference](https://github.com/openaiotlab/contextagent/blob/main/src/sft/inference.py): `src/sft/inference.py`
- [Run mode](https://github.com/openaiotlab/contextagent/blob/main/src/config.py): `src/config.py`
- [Score results](https://github.com/openaiotlab/contextagent/blob/main/src/calculate_scores.py): `src/calculate_scores.py`
- [Inference utilities](https://github.com/openaiotlab/contextagent/blob/main/src/utils.py): `src/utils.py`

### Context tools

- [Tool registry](https://github.com/openaiotlab/contextagent/blob/main/src/tool_registry.py): `src/tool_registry.py`
- [Context tools](https://github.com/openaiotlab/contextagent/tree/main/src/tools): `src/tools`

### LLaMA-Factory

- [Training workflows](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/train): `LLaMA-Factory/src/llamafactory/train`
- [Data processing](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/data): `LLaMA-Factory/src/llamafactory/data`
- [Chat engines](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/chat): `LLaMA-Factory/src/llamafactory/chat`
- [Model loading](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/model): `LLaMA-Factory/src/llamafactory/model`
- [Quantization](https://github.com/openaiotlab/contextagent/blob/main/LLaMA-Factory/src/llamafactory/model/model_utils/quantization.py): `LLaMA-Factory/src/llamafactory/model/model_utils/quantization.py`
- [Run parameters](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/hparams): `LLaMA-Factory/src/llamafactory/hparams`
- [Inference API](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/api): `LLaMA-Factory/src/llamafactory/api`
- [Web interface](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/webui): `LLaMA-Factory/src/llamafactory/webui`
- [Runtime support](https://github.com/openaiotlab/contextagent/tree/main/LLaMA-Factory/src/llamafactory/extras): `LLaMA-Factory/src/llamafactory/extras`

### Other components

- **Evaluator**
- **External APIs**

## Connections

- Evaluator → Local ICL: runs ICL (evidence: `README.md`)
- Evaluator → API ICL: runs API ICL (evidence: `README.md`)
- Evaluator → SFT inference: evaluates SFT (evidence: `README.md`)
- Local ICL → Tool registry: loads tools (evidence: `src/icl/inference.py`)
- API ICL → Tool registry: loads tools (evidence: `src/icl/inference_api.py`)
- SFT inference → Tool registry: loads tools (evidence: `src/sft/inference.py`)
- Local ICL → Run mode: uses mode (evidence: `src/icl/inference.py`)
- API ICL → Run mode: uses mode (evidence: `src/icl/inference_api.py`)
- SFT inference → Run mode: uses mode (evidence: `src/sft/inference.py`)
- Tool registry → Context tools: registers tools (evidence: `src/tool_registry.py`)
- Context tools → External APIs: calls integrations (evidence: `README.md`)
- Evaluator → Score results: scores runs (evidence: `README.md`)
- Local ICL → Inference utilities: uses utilities (evidence: `src/icl/inference.py`)
- API ICL → Inference utilities: uses utilities (evidence: `src/icl/inference_api.py`)
- SFT inference → Inference utilities: uses utilities (evidence: `src/sft/inference.py`)
- Evaluator → Training workflows: launches SFT (evidence: `README.md`)
- Training workflows → Data processing: uses data pipeline
- Training workflows → Model loading: loads models
- Chat engines → Data processing: uses templates (evidence: `LLaMA-Factory/src/llamafactory/chat/base_engine.py`)
- Chat engines → Model loading: loads model (evidence: `LLaMA-Factory/src/llamafactory/chat/hf_engine.py`)
- Chat engines → Quantization: configures quantization (evidence: `LLaMA-Factory/src/llamafactory/chat/sglang_engine.py`)
- Inference API → Chat engines: selects engine (evidence: `LLaMA-Factory/src/llamafactory/api/app.py`)
- Web interface → Chat engines: uses chat engine (evidence: `LLaMA-Factory/src/llamafactory/webui/chatter.py`)
- Chat engines → Run parameters: accepts arguments (evidence: `LLaMA-Factory/src/llamafactory/chat/base_engine.py`)
