Creating Hierarchical AI Assistant Contexts: Global vs. Project-Specific Configurations
Source: Dev.to
Introduction
This article stems from a real challenge I encountered while developing and managing an AI assistant that needed to operate effectively across multiple contexts. The problem emerged when the same assistant had to handle both general system‑administration tasks and highly specialized project work within specific codebases.
Initially, the AI assistant experienced context confusion—responding with general knowledge when working on specialized projects, or applying project‑specific rules inappropriately to general tasks. This led to inconsistent behavior and reduced effectiveness.
The solution I developed involved creating a robust separation and hierarchy between global and project‑specific AI assistant configurations, ensuring context‑aware behavior without confusion. This approach allows the assistant to maintain its core safety and operational principles while specializing its behavior based on the current project context.
The Problem: Context Confusion
Without proper configuration hierarchies, AI assistants can experience identity confusion when switching between different scopes of work. For example, an assistant might:
- Respond with general system‑administration knowledge while working on a specific software project, or
- Apply project‑specific rules to a general task.
Both scenarios lead to inconsistent behavior and reduced effectiveness.
Solution Overview
The solution involves creating layered configuration files that establish clear priority and context‑switching mechanisms:
- Global Configuration – Base rules that apply everywhere.
- Project‑Specific Configuration – Specialized rules for individual projects.
- Dynamic Context Detection – Automatic switching based on the current directory.
- Clear Hierarchy – Well‑defined priority when conflicts arise.
Implementation Strategy
1. Global Configuration Foundation
The global configuration serves as the base layer with essential rules that apply universally:
# global-config.yml
assistant:
name: "Universal AI Assistant"
safety:
enabled: true
policies:
- no-harm
- privacy-first
logging:
level: info
destination: /var/log/assistant/global.log
defaults:
temperature: 0.7
max_tokens: 1500
Explanation
- Safety policies are enforced everywhere.
- Logging is centralized for audit purposes.
- Defaults provide a consistent baseline for all interactions.
2. Project‑Specific Configuration
Each project can override or extend the global settings:
# .assistant/project-config.yml
assistant:
name: "Project‑X AI Assistant"
defaults:
temperature: 0.3 # More deterministic for code generation
max_tokens: 800
extensions:
- linting-helper
- test‑generator
context:
root: "./src"
language: "python"
Key points
- Overrides
temperaturefor more predictable outputs. - Adds project‑specific extensions (e.g., linting, test generation).
- Defines the project root and primary language for dynamic context detection.
3. Dynamic Context Detection
A small wrapper script detects the current working directory and loads the appropriate configuration:
#!/usr/bin/env python3
import os
import yaml
def load_config():
cwd = os.getcwd()
# Walk up the directory tree looking for a project config
while cwd != "/":
proj_cfg_path = os.path.join(cwd, ".assistant", "project-config.yml")
if os.path.isfile(proj_cfg_path):
with open(proj_cfg_path) as f:
return yaml.safe_load(f)
cwd = os.path.dirname(cwd)
# Fallback to global config
with open("/etc/assistant/global-config.yml") as f:
return yaml.safe_load(f)
config = load_config()
print(f"Loaded configuration for: {config['assistant']['name']}")
The script:
- Starts at the current directory.
- Traverses upward until it finds a
.assistant/project-config.yml. - Falls back to the global configuration if none is found.
4. Clear Hierarchy & Conflict Resolution
When both global and project configurations define the same key, the project‑specific value wins. This can be enforced programmatically:
def merge_configs(global_cfg, project_cfg):
merged = global_cfg.copy()
for key, value in project_cfg.items():
if isinstance(value, dict) and key in merged:
merged[key] = merge_configs(merged[key], value)
else:
merged[key] = value
return merged
The merging strategy ensures:
- Deep merging for nested dictionaries.
- Project‑level overrides take precedence.
Benefits
- Predictable behavior: The assistant knows which rule set to apply based on context.
- Safety first: Global safety policies are never bypassed.
- Scalability: Adding new projects only requires a small config file.
- Maintainability: Centralized global settings reduce duplication.
Conclusion
By establishing a clear hierarchy—global defaults, project‑specific overrides, and dynamic detection—you can eliminate context confusion and provide a reliable, safe, and adaptable AI assistant across diverse workloads. This pattern is language‑agnostic and can be adapted to any tooling ecosystem that supports hierarchical configuration files.
Every Session Protocol
- Read core identity files (
SOUL.md,USER.md) - Load memory context
- Check for project‑specific configs in the current directory
- Apply project rules with priority when applicable
- Report current‑directory context when relevant
2. Project‑Specific Configuration
Each project maintains its own configuration files that specialize the assistant’s behavior:
| File | Purpose |
|---|---|
| SOUL.md | Project‑specific personality and priorities |
| IDENTITY.md | Role‑specific identity and communication style |
| USER.md | Project team members and their preferences |
| AGENTS.md | Project‑specific workflows and processes |
| MEMORY.md | Important project notes (optional) |
3. Dynamic Context Detection
The assistant should automatically detect and adapt to the current project context.
// Pseudocode for context detection
if (current_directory.hasProjectConfigs()) {
const projectIdentity = loadProjectIdentity(current_directory);
const globalRules = loadGlobalRules();
return mergeConfigs(projectIdentity, globalRules, { priority: "project" });
} else {
return loadGlobalRules();
}
4. Clear Hierarchy and Conflict Resolution
| Domain | Rule |
|---|---|
| Development Tasks | Project rules take priority |
| Safety / Ethics | Global safety rules always take precedence |
| Operational Tasks | Apply both sets of rules appropriately |
| Identity Questions | Present project role first, then general capabilities |
Best Practices
1. Consistent File Structure
Maintain the same configuration file layout across all projects:
SOUL.md– personality and valuesIDENTITY.md– specific role definitionUSER.md– team member informationAGENTS.md– workflow and processesMEMORY.md– important project notes (optional)
2. Explicit Context Reporting
- Report the current directory when relevant.
- Clarify your role based on the detected context.
- Explain when you switch between contexts.
3. Seamless Transitions
- Detect context changes automatically.
- Apply the appropriate rules without user intervention.
- Preserve continuity across context switches.
4. Safety‑First Approach
- Global safety rules override any project‑specific rules.
- Maintain privacy protections regardless of context.
- Preserve core ethical constraints at all times.
Benefits of This Approach
- Context‑Aware Behavior – Responses adapt to the current project.
- Reduced Confusion – Clear identity and role definitions prevent mixing contexts.
- Scalability – New projects can be added with their own specialized configurations.
- Consistency – Core safety and operational principles remain intact.
- Flexibility – Handles both specialized and general tasks effectively.
Real‑World Example
Scenario: An AI assistant works on system administration and a specific web‑application project.
| Context | Focus |
|---|---|
| Global | System operations, file management, general productivity |
| Web‑App Project | Coding, testing, deployment, project‑specific workflows |
- Identity – Always explain both general capabilities and the project‑specific role.
- Safety – The same security and privacy measures apply in both contexts.
Conclusion
A hierarchical AI‑assistant configuration system enables specialized project work while retaining general utility. By implementing automatic context detection, clear priority rules, and seamless transitions, assistants become more effective in specific domains and remain reliable for general tasks. The key is to design the system with:
- Automatic context detection
- Explicit hierarchy (project > global, with safety always on top)
- Transparent reporting of the active context
This structure enhances the user experience without adding unnecessary complexity.
