Skip to content
Learn Agentic AI11 min read0 views

Dynamic Agent Configuration: Updating Behavior Without Redeployment

Master dynamic configuration for AI agents using config stores, hot reload patterns, validation, and audit trails. Update prompts, models, and tools without restarting services.

The Redeployment Problem

Every time you change a system prompt, adjust a temperature setting, or swap a model, you face a choice: redeploy the entire service or find a way to update configuration at runtime. For AI agents, redeployment means downtime, cold starts, and interrupted conversations. Dynamic configuration eliminates this friction by separating agent behavior from agent code.

The key insight is that most of what makes an AI agent behave a certain way — its system prompt, model selection, tool configuration, guardrail thresholds — is data, not code. Treat it as data and you gain the ability to tune agent behavior in seconds instead of minutes.

Config Store Architecture

A production-grade config store needs versioning, validation, and change notifications. Here is a design built on top of Redis with a PostgreSQL audit log.

import json
import time
from dataclasses import dataclass
from typing import Any, Optional
import redis
import hashlib


@dataclass
class ConfigVersion:
    version: int
    data: dict[str, Any]
    checksum: str
    updated_by: str
    updated_at: float


class AgentConfigStore:
    def __init__(self, redis_url: str, namespace: str = "agent_config"):
        self._redis = redis.from_url(redis_url)
        self._namespace = namespace

    def _key(self, agent_id: str) -> str:
        return f"{self._namespace}:{agent_id}"

    def get(self, agent_id: str) -> Optional[ConfigVersion]:
        raw = self._redis.get(self._key(agent_id))
        if raw is None:
            return None
        data = json.loads(raw)
        return ConfigVersion(**data)

    def put(
        self,
        agent_id: str,
        config: dict[str, Any],
        updated_by: str,
    ) -> ConfigVersion:
        current = self.get(agent_id)
        new_version = (current.version + 1) if current else 1
        checksum = hashlib.sha256(
            json.dumps(config, sort_keys=True).encode()
        ).hexdigest()[:12]

        version = ConfigVersion(
            version=new_version,
            data=config,
            checksum=checksum,
            updated_by=updated_by,
            updated_at=time.time(),
        )
        self._redis.set(
            self._key(agent_id),
            json.dumps(version.__dict__),
        )
        self._publish_change(agent_id, new_version)
        return version

    def _publish_change(self, agent_id: str, version: int):
        self._redis.publish(
            f"{self._namespace}:changes",
            json.dumps({"agent_id": agent_id, "version": version}),
        )

Hot Reload with Change Listeners

The config store publishes change events on a Redis pub/sub channel. Agent instances subscribe and reload their configuration without restarting.

import threading


class ConfigWatcher:
    def __init__(self, store: AgentConfigStore, agent_id: str):
        self._store = store
        self._agent_id = agent_id
        self._current: Optional[ConfigVersion] = None
        self._callbacks: list = []
        self._running = False

    def on_change(self, callback):
        self._callbacks.append(callback)

    def start(self):
        self._current = self._store.get(self._agent_id)
        self._running = True
        thread = threading.Thread(target=self._listen, daemon=True)
        thread.start()

    def _listen(self):
        pubsub = self._store._redis.pubsub()
        pubsub.subscribe(f"{self._store._namespace}:changes")
        for message in pubsub.listen():
            if not self._running:
                break
            if message["type"] != "message":
                continue
            event = json.loads(message["data"])
            if event["agent_id"] == self._agent_id:
                self._current = self._store.get(self._agent_id)
                for cb in self._callbacks:
                    cb(self._current)

    def stop(self):
        self._running = False

Configuration Validation

Never apply configuration without validation. A malformed prompt or an invalid model name can crash the agent or produce garbage output.

See AI Voice Agents Handle Real Calls

Book a free demo or calculate how much you can save with AI voice automation.

from pydantic import BaseModel, field_validator
from typing import Literal


class AgentConfig(BaseModel):
    system_prompt: str
    model: str
    temperature: float
    max_tokens: int
    tools: list[str]
    guardrail_threshold: float

    @field_validator("temperature")
    @classmethod
    def validate_temperature(cls, v: float) -> float:
        if not 0.0 <= v <= 2.0:
            raise ValueError("Temperature must be between 0.0 and 2.0")
        return v

    @field_validator("system_prompt")
    @classmethod
    def validate_prompt_not_empty(cls, v: str) -> str:
        if len(v.strip()) < 20:
            raise ValueError("System prompt must be at least 20 characters")
        return v

    @field_validator("tools")
    @classmethod
    def validate_tools(cls, v: list[str]) -> list[str]:
        allowed = {"search", "calculator", "code_interpreter", "file_reader"}
        invalid = set(v) - allowed
        if invalid:
            raise ValueError(f"Unknown tools: {invalid}")
        return v


def safe_update(store: AgentConfigStore, agent_id: str, raw: dict, user: str):
    config = AgentConfig(**raw)
    return store.put(agent_id, config.model_dump(), updated_by=user)

Audit Trail

Every configuration change should be logged with who changed what, when, and what the previous value was. This is essential for debugging regressions.

from datetime import datetime


class ConfigAuditLog:
    def __init__(self, db_connection):
        self._db = db_connection

    async def log_change(
        self,
        agent_id: str,
        old_version: Optional[ConfigVersion],
        new_version: ConfigVersion,
    ):
        await self._db.execute(
            """
            INSERT INTO config_audit_log
                (agent_id, old_version, new_version, old_checksum,
                 new_checksum, changed_by, changed_at, old_data, new_data)
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
            """,
            agent_id,
            old_version.version if old_version else 0,
            new_version.version,
            old_version.checksum if old_version else None,
            new_version.checksum,
            new_version.updated_by,
            datetime.fromtimestamp(new_version.updated_at),
            json.dumps(old_version.data) if old_version else None,
            json.dumps(new_version.data),
        )

Putting It Together

Here is how the pieces connect in a FastAPI application.

from fastapi import FastAPI, BackgroundTasks

app = FastAPI()
store = AgentConfigStore(redis_url="redis://localhost:6379/0")
watcher = ConfigWatcher(store, agent_id="support-agent")


def on_config_updated(new_config: ConfigVersion):
    print(f"Config updated to v{new_config.version} [{new_config.checksum}]")


watcher.on_change(on_config_updated)
watcher.start()


@app.get("/agent/config")
def get_config():
    current = store.get("support-agent")
    return {"version": current.version, "config": current.data}

FAQ

How do I handle config changes mid-conversation?

Load configuration at the start of each conversation turn, not once per session. This way new config takes effect on the next user message without disrupting the current exchange. For long-running conversations, you can pin the config version to avoid mid-conversation behavior shifts.

What happens if the config store is unavailable?

Always cache the last known good configuration locally. If Redis is unreachable, fall back to the cached version and emit an alert. The agent should never fail to respond because the config store is temporarily down.

How do I roll back a bad configuration change?

Since every version is stored in the audit log with its full data payload, rolling back is just a matter of writing the old version's data as a new version. This preserves the full change history rather than silently overwriting.


#DynamicConfiguration #AIAgents #HotReload #ConfigManagement #Python #AgenticAI #LearnAI #AIEngineering

Share this article
C

CallSphere Team

Expert insights on AI voice agents and customer communication automation.

Try CallSphere AI Voice Agents

See how AI voice agents work for your industry. Live demo available -- no signup required.