Agent Architecture
Credo’s agent architecture is built on three core classes:Agent Class
TheAgent class is the main entry point for applications. It extends BaseAgent and handles initialization, configuration, and lifecycle management.
The
Agent class requires platform-specific dependencies (agentDependencies) which provide implementations for cryptography, networking, and storage operations.BaseAgent Class
TheBaseAgent is an abstract base class that provides core functionality:
packages/core/src/agent/BaseAgent.ts:18
AgentConfig Class
TheAgentConfig class holds the agent’s configuration and provides access to important settings:
packages/core/src/agent/AgentConfig.ts:7
Configuration Options
InitConfig Interface
TheInitConfig interface defines the configuration options for the agent:
packages/core/src/types.ts:4
Configuration Options:
logger: Custom logger implementation (defaults to ConsoleLogger with LogLevel.off)autoUpdateStorageOnStartup: Automatically migrate storage when starting the agent (default: false)allowInsecureHttpUrls: Allow HTTP URLs in production (use with caution, default: false)validitySkewSeconds: Clock skew tolerance for JWT validation in seconds (default: 30)
Agent Lifecycle
Initialization
The agent must be initialized before use:- Initializes all registered modules
- Initializes the root agent context
- Checks storage version and performs migrations if needed
- Sets up event listeners and services
packages/core/src/agent/Agent.ts:75
Shutdown
Properly shut down the agent when done:- Emits a stop signal to all observables
- Shuts down all registered modules
- Closes the agent context
- Cleans up resources
packages/core/src/agent/Agent.ts:113
AgentContext
TheAgentContext provides a scoped context for operations:
packages/core/src/agent/context/AgentContext.ts:6
Context Features
- Dependency Resolution: Access to all registered services and modules
- Context Correlation: Unique identifier for tracking operations across sessions
- Multi-tenancy Support: Root context vs. tenant-specific contexts
- Session Management: Proper cleanup of resources
Event System
The agent provides an event emitter for observing operations:All observables automatically stop when the agent is shut down via the internal
stop$ observable.Dependency Injection
Credo uses a dependency injection system to manage services:Built-in Services
The agent automatically registers:EventEmitter: Event systemJwsService: JSON Web Signature operationsStorageVersionRepository: Storage version trackingStorageUpdateService: Storage migration management- All configured modules and their services
packages/core/src/agent/Agent.ts:29
Multi-tenancy
Credo supports multi-tenancy through theTenantModule (separate package). Each tenant gets its own isolated AgentContext with separate storage while sharing the root configuration.
Best Practices
Initialization:
- Always initialize the agent before performing operations
- Handle storage migrations appropriately in production
- Use proper error handling during initialization
- Always call
shutdown()when done to clean up resources - Ensure all pending operations complete before shutdown
- Use try-finally blocks to ensure cleanup
- Never use
allowInsecureHttpUrls: truein production - Set appropriate validity skew for your use case
- Configure logging level based on environment
Example: Complete Agent Setup
Related Topics
- Modules - Learn about the module system
- Storage - Understand storage architecture
- DID Methods - Supported DID methods