File size: 3,341 Bytes
e6896d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2e0e38
 
 
 
e6896d0
96efa83
 
 
b2e0e38
 
 
 
 
 
 
 
 
 
e6896d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2e0e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6896d0
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
library_name: transformers
tags:
- agents
- offline-first
- edge-computing
- context-aware
- global-south
- low-resource-nlp
license: mit
language:
- en
pipeline_tag: text-generation
---



# Contextual Engineering Patterns: Architecting Adaptable AI Agents

[![License: CC BY 4.0](https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by/4.0/)
[![Status: Open Access](https://img.shields.io/badge/Status-Open%20Access-green.svg)]()
[![Paper: AfricArXiv](https://img.shields.io/badge/Paper-Read%20Preprint-red)](https://africarxiv.ubuntunet.net/items/2af79f5d-ce68-4050-8b25-3bc9128c7232)
[![Book: Published](https://img.shields.io/badge/Book-Read%20Full%20Text-blue)](https://zenodo.org/records/18005435) 

> **Reference implementations for the architectural patterns defined in the book *"Contextual Engineering: Architecting Adaptable AI Agents for the Real World"* by Tobi Lekan Adeosun.**

## πŸ“– Overview

Standard AI agents are designed for the "Abundance Baseline" of Silicon Valleyβ€”perfect internet, unlimited power, and institutional trust. When deployed in the Global South, these agents fail due to the **"Agentic Gap"** between their reasoning capabilities and environmental realities.

This repository contains the **Python reference implementations** for the three core adaptation layers introduced in the book:
1.  **Infrastructure Adapter:** Handling offline states and compute scarcity.
2.  **Cultural Adapter:** Managing semantic drift and high-context communication.
3.  **Safety Adapter:** Enforcing constitutional guardrails and Human-in-the-Loop (HITL) workflows.

## ⚑ Quick Start (Hybrid Router)

How to use the **Infrastructure Adapter** to route traffic based on connectivity:

```python
from src.infrastructure.inference_router import HybridRouter

# Initialize router with cost/latency preferences
router = HybridRouter(preference="economy", offline_fallback=True)

# The router automatically checks network status (N(t))
model_choice = router.select_model(
    prompt="Summarize this contract",
    complexity_score=0.85
)

print(f"Routing to: {model_choice}")
# Output: "Llama-3-8B-Local" (if offline) or "GPT-4o" (if online)

## πŸ“‚ Repository Structure

The code is organized by the "Adapter Layer" it serves, matching the chapters of the manuscript.

```text
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ infrastructure
β”‚   β”‚   β”œβ”€β”€ sync_manager.py       # (Chapter 3) The "Sync-Later" Architecture & Offline Queue
β”‚   β”‚   └── inference_router.py   # (Chapter 4) The Hybrid Router (Local vs. Cloud)
β”‚   β”œβ”€β”€ safety
β”‚   β”‚   β”œβ”€β”€ sentinel.py           # (Chapter 9) Constitutional Safety Checks & Kill Switches
β”‚   β”‚   └── escalation_ladder.py  # (Chapter 10) Human-in-the-Loop Risk Evaluation Logic
β”‚   └── culture
β”‚       └── context_injector.py   # (Chapter 6) Dynamic Few-Shot Prompting logic
└── README.md


## Citation

If you use this framework in your research, please cite the associated whitepaper:

```bibtex
@article{adeosun2026contextual,
  title={Contextual Engineering: Architectural Patterns for Resilient AI Agents},
  author={Adeosun, Tobi},
  journal={AfricArXiv},
  year={2026},
  url={[https://osf.io/preprints/africarxiv/](https://osf.io/preprints/africarxiv/)[YOUR_HANDLE]}
}