| name | ak-test |
| description | Set up testing and debug common issues in Agent Kernel projects. This skill guides you through configuring the built-in test framework, writing agent tests, choosing test modes (fuzzy, judge, fallback), and troubleshooting common errors.
|
| license | Apache-2.0 |
| metadata | {"author":"yaalalabs","category":"user"} |
Testing & Debugging
Use this skill to set up testing for your Agent Kernel project or debug issues.
Instructions for the Agent
Setting Up Tests
1. Add Test Dependencies
Update pyproject.toml:
[dependency-groups]
dev = [
"agentkernel[test]>=0.6.1",
"black>=23.0.0",
"isort>=5.0.0",
"mypy>=1.0.0",
]
Run uv sync to install test dependencies.
2. Choose a Test Mode
Update config.yaml:
test:
mode: fuzzy
| Mode | How it Works | Best For |
|---|
| fuzzy | String similarity matching (rapidfuzz) | Deterministic responses, exact answers |
| judge | LLM evaluates if response is semantically correct | Open-ended responses, creative agents |
| fallback | Tries fuzzy first, falls back to judge if fuzzy fails | General-purpose testing |
For judge mode, configure the judge model:
test:
mode: judge
judge:
model: gpt-4o-mini
3. Write CLI Agent Tests
For agents running via CLI (demo.py):
import pytest
import pytest_asyncio
from agentkernel.test import Test
pytestmark = pytest.mark.asyncio(loop_scope="session")
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def test_client():
test = Test("demo.py")
await test.start()
try:
yield test
finally:
await test.stop()
@pytest.mark.order(1)
async def test_greeting(test_client):
await test_client.send("Hello!")
await test_client.expect(["Hello", "Hi", "Greetings"])
@pytest.mark.order(2)
async def test_specific_question(test_client):
await test_client.send("What is the capital of France?")
await test_client.expect(["Paris"])
@pytest.mark.order(3)
async def test_follow_up(test_client):
await test_client.send("What is its population?")
await test_client.expect(["2 million", "2.1 million", "approximately 2 million"])
Key patterns:
- Use
@pytest.mark.order(n) for sequential tests where context matters
- Use
scope="session" fixtures so the agent stays running across tests
expect() takes a list of acceptable answer patterns
- The test framework uses the configured mode to compare responses
4. Write API Agent Tests
For agents running via REST API:
import asyncio
import os
import subprocess
import sys
import uuid
import httpx
import pytest
import pytest_asyncio
from agentkernel.test import Test
pytestmark = pytest.mark.asyncio(loop_scope="session")
class APITestClient:
def __init__(self, url: str):
self.url = url
self.session_id = str(uuid.uuid4())
async def send(self, prompt: str, agent: str = "triage") -> str:
payload = {
"prompt": prompt,
"session_id": self.session_id,
"agent": agent,
}
async with httpx.AsyncClient(timeout=30.0) as client:
resp = await client.post(f"{self.url}/run", json=payload)
resp.raise_for_status()
return resp.json().get("result", "")
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def http_client():
endpoint = os.getenv("AK_TEST_ENDPOINT", "http://localhost:8000")
yield APITestClient(endpoint)
@pytest.mark.order(1)
async def test_basic_question(http_client):
response = await http_client.send("What is 2+2?")
Test.compare(response, ["4", "The answer is 4"])
@pytest.mark.order(2)
async def test_agent_routing(http_client):
response = await http_client.send("Tell me about World War 2")
Test.compare(response, ["World War II", "World War 2", "WWII"])
5. Run Tests
uv run pytest
uv run pytest demo_test.py
uv run pytest -k "test_greeting"
uv run pytest -x
uv run pytest -v
uv run pytest --tb=long
Debugging Common Issues
Issue: "No agents available"
Symptom: CLI shows "No agents available. Please load an agent module using !load."
Cause: The Module constructor was not called, so no agents are registered with Runtime.
Fix: Ensure your agent file calls the Module constructor:
OpenAIModule([triage_agent, math_agent])
Issue: Session state not persisting
Symptom: Agent doesn't remember context from previous messages.
Causes & Fixes:
- In-memory sessions (default): State is lost when the process restarts. Switch to Redis/DynamoDB/CosmosDB for persistence.
- Different session IDs: Ensure you're using the same
session_id across requests.
- Lambda cold starts: Session state must be stored externally. Use Redis or DynamoDB.
Check session config:
session:
type: redis
redis:
url: "redis://localhost:6379"
prefix: "ak:myproject:"
Issue: "ToolContext not available"
Symptom: RuntimeError: ToolContext is not set inside a tool function.
Cause: The tool is being called outside of the agent execution context.
Fix: Ensure tool functions are bound via the framework's ToolBuilder and called within agent execution. Don't call tool functions directly outside of Runtime.run().
tools = OpenAIToolBuilder.bind([my_tool])
agent = Agent(name="test", tools=tools, instructions="...")
def my_tool(query: str) -> str:
context = ToolContext.get()
session = context.session
return "result"
Issue: Guardrail blocks all requests
Symptom: Every request returns a guardrail violation message.
Fixes:
- Check guardrail config — ensure
config_path points to a valid JSON file
- Review guardrail rules — thresholds may be too strict
- Check the guardrail model — ensure
model field is correct
- Disable temporarily to isolate: set
enabled: false in config
Issue: Import errors for framework packages
Symptom: ModuleNotFoundError: No module named 'agents' (or crewai, langgraph, etc.)
Fix: Install the correct extras:
pip install "agentkernel[openai]"
pip install "agentkernel[crewai]"
pip install "agentkernel[langgraph]"
pip install "agentkernel[adk]"
Or in pyproject.toml:
dependencies = ["agentkernel[openai,api]>=0.6.1"]
Issue: Redis connection errors
Symptom: ConnectionError: Error connecting to Redis or similar.
Fixes:
- Verify Redis is running:
redis-cli ping should return PONG
- Check the URL in config:
redis://host:port format
- For AWS ElastiCache: ensure your app is in the same VPC
- Check security groups / firewall rules
Issue: Terraform deployment fails
Symptom: terraform apply errors out.
Common fixes:
- Run
terraform init first
- Check AWS/Azure credentials:
aws sts get-caller-identity or az account show
- Verify the Terraform module version matches your
agentkernel version
- Check that required variables are set in
terraform.tfvars
- For state conflicts:
terraform state list and terraform state rm to clean up
Issue: Webhook not receiving messages
Symptom: Messages sent on Slack/WhatsApp/etc. don't reach the agent.
Fixes:
- Verify webhook URL is publicly accessible (use ngrok for local dev:
ngrok http 8000)
- Check platform webhook configuration points to the correct path:
- Slack:
/slack/events
- WhatsApp:
/whatsapp/webhook
- Telegram:
/telegram/webhook
- Verify environment variables (bot tokens, signing secrets)
- Check server logs for incoming webhook requests
- Test health endpoint:
curl http://localhost:8000/health
Enabling Debug Logging
Add to config.yaml:
logging:
ak:
level: DEBUG
system:
level: DEBUG
Or set environment variables:
export AK_LOGGING__AK__LEVEL=DEBUG
export AK_LOGGING__SYSTEM__LEVEL=DEBUG
logging.ak.level controls Agent Kernel's own logger verbosity
logging.system.level controls the process-wide/root logger (use with caution as it affects all application logging)
- If you do not want Agent Kernel to modify application-wide logging, omit the
system section
Health Check Endpoint
All API-mode agents expose a health endpoint:
curl http://localhost:8000/health
Use this to verify your server is running and accessible.
What to Do Next
Your tests are set up and passing. Here's what you might do next:
- Add more tools & agents → Use the
ak-build skill to iterate on your project — add new capabilities, then come back here to add tests for them.
- Deploy to cloud → Use the
ak-cloud-deploy skill to deploy your tested agent to AWS or Azure.
- Add guardrails → Use the
ak-add-capabilities skill to add input/output guardrails, tracing, or session persistence.
- Connect a messaging platform → Use the
ak-add-integration skill to make your tested agent available on Slack, WhatsApp, or other channels.