> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

Create reusable **named agents** for workflow pipelines.

## Intent classification agent

```python
import json
from dnotifier import define_agent, WorkflowError

def extract_content(response) -> str:
    if isinstance(response, dict) and isinstance(response.get("data"), dict):
        return str(response["data"].get("content", ""))
    return str(response)

intent_agent = define_agent(
    name="intent-agent",
    run=lambda ctx: _run_intent_agent(ctx),
)

async def _run_intent_agent(ctx):
    input_data = ctx.input
    if isinstance(input_data, str):
        text = input_data
    elif isinstance(input_data, dict):
        text = str(input_data.get("text") or input_data.get("question") or "")
    else:
        text = str(input_data)

    if not text.strip():
        raise WorkflowError("intent-agent: empty input", agent_name="intent-agent")

    ai_response = await ctx.send_ai(
        message={
            "useKnowledgeBase": False,
            "messages": [
                {
                    "role": "system",
                    "content": (
                        'Classify the user message. Reply JSON only: {"intent":"search"} '
                        'for documentation lookup, or {"intent":"general"} for general chat.'
                    ),
                },
                {"role": "user", "content": text},
            ],
        },
        save_history=False,
        label="Intent classification",
    )

    content = extract_content(ai_response)
    try:
        parsed = json.loads(content)
    except json.JSONDecodeError:
        parsed = {
            "intent": "search" if "search" in content.lower() else "general",
        }

    intent = "search" if parsed.get("intent") == "search" else "general"
    ctx.state["last_intent"] = intent

    return {"intent": intent, "raw": parsed}
```

## General Q\&A agent

```python
general_agent = define_agent(
    name="general-agent",
    run=lambda ctx: _run_general_agent(ctx),
)

async def _run_general_agent(ctx):
    input_data = ctx.input
    if isinstance(input_data, str):
        question = input_data
    elif isinstance(input_data, dict):
        question = input_data.get("question")
    else:
        question = str(input_data)

    ai_response = await ctx.send_ai(
        message={
            "useKnowledgeBase": False,
            "messages": [
                {"role": "system", "content": "You are a helpful assistant. Answer concisely."},
                {"role": "user", "content": question or ""},
            ],
        },
        save_history=False,
        label="General Q&A",
    )

    answer = extract_content(ai_response)
    ctx.state["general_answer"] = answer
    return {"answer": answer}
```

## Tool agent (no custom `run`)

```python
async def add_numbers(input_data, context):
    return {"sum": float(input_data["a"]) + float(input_data["b"])}

add = notifier.tools.register(
    {
        "name": "math.add",
        "description": "Add two numbers",
        "inputSchema": {
            "type": "object",
            "properties": {"a": {"type": "number"}, "b": {"type": "number"}},
            "required": ["a", "b"],
        },
        "execute": add_numbers,
    }
)

agent = define_agent(
    name="math-assistant",
    instructions="Use math.add when the user asks to add numbers.",
    provider="open_ai",
    model="gpt-4o",
    tools=[add],
    permissions={"math.add": ["execute"]},
)

result = await notifier.run_agent(agent, input="What is 21 + 21?")
```

Full walkthrough: [**Tools & MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/python.md) · [**Local tool example**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-tool-agent/python.md)

## Learn more

* [**Tools & MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/python.md)
* [**Build a workflow**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/build-workflow/python.md)
* [**Example: Intent router**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-intent-router/python.md)
