Monitoring and Reliability › Deeper Health Checks
AI Agent HandbookAll chapters Monitoring and Reliability › Deeper Health ChecksOne section · Sections in this chapter4 / 14 ↓
A basic health check tells you the web server is responding, but your agent depends on more than just the web framework. It needs to call the language model API, access any databases or memory stores, and use external tools. A more thorough health check verifies these dependencies too.
Here's an enhanced health check that tests key components:
@app.route('/health', methods=['GET'])
def health_check():
"""Comprehensive health check"""
health_status = {
"status": "healthy",
"timestamp": time.time(),
"checks": {}
}
# Check if we can reach the LLM API
try:
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
# Make a minimal API call to verify connectivity
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=10,
messages=[{"role": "user", "content": "test"}]
)
health_status["checks"]["llm_api"] = "ok"
except Exception as e:
health_status["checks"]["llm_api"] = f"error: {str(e)}"
health_status["status"] = "degraded"
# Check database connectivity
try:
# Test database connection
db.execute("SELECT 1")
health_status["checks"]["database"] = "ok"
except Exception as e:
health_status["checks"]["database"] = f"error: {str(e)}"
health_status["status"] = "degraded"
return jsonify(health_status)This health check tests the critical dependencies. If the LLM API is unreachable or the database is down, the health check reports "degraded" status. You know the agent is running, but it can't function properly.
Be careful with comprehensive health checks, though. You don't want the health check itself to be expensive or slow. Keep the checks lightweight. A minimal API call to verify connectivity is fine, but don't run a full agent query as part of the health check.