Agent SDK · Running agents

Agent.

Define named, reusable personas with instructions, purpose, and metadata.

An Agent is a named, reusable persona with optional instructions, context, purpose, and metadata. You can pass a plain dict with the same keys anywhere an Agent is accepted — the SDK normalises it.

Agent class

Python
from matilda_agent_sdk import Agent

reviewer = Agent(
    name="code-reviewer",
    purpose="code",
    instructions="Review code for correctness, security, and readability.",
    context="Project: matilda-core\nLanguage: Python",
    metadata={"team": "platform"},
)

The Agent is reusable — construct once, run many times.

Constructor parameters

Fieldtypedescription
namestrAgent name. Must be non-empty (raises ValueError otherwise). Required.
instructionsAgentInstructionsStatic string or dynamic callable (see below).
contextstr | NoneAdditional context appended to instructions under a Context: header.
purposeAgentPurposeControls the default response_mode and how the server routes the request. Defaults to 'code'.
response_modestr | NoneOverride the response mode. If omitted, derived from purpose.
metadatadictCustom data available to dynamic instructions. Defaults to {}.

AgentPurpose

Python
AgentPurpose = Literal["code", "analysis", "general"]
Purposeresponse_mode default
'code''auto'
'analysis''deep'
'general''auto'

Dynamic instructions

Instructions can be a callable that receives a runtime context dict, letting you customise behaviour per-call (async callables are awaited):

Python
from matilda_agent_sdk import Agent, run


def reviewer_instructions(ctx):
    lang = ctx["metadata"].get("language", "auto-detect")
    strictness = ctx["metadata"].get("strictness", "normal")
    return "\n".join(
        [
            "Review the following code.",
            f"Language: {lang}",
            f"Strictness: {strictness}",
            "Focus on: correctness, security, and readability.",
        ]
    )


agent = Agent(name="code-reviewer", purpose="code", instructions=reviewer_instructions)


async def main():
    result = await run(
        agent,
        "def add(a, b): return a + b",
        metadata={"language": "Python", "strictness": "strict"},
    )
    print(result.final_output)

AgentInstructions

Python
AgentInstructions = str | Callable[[dict], str | Awaitable[str]]

The context dict passed to the callable:

Python
{
    "agent_name": agent.name,
    "input": trimmed_prompt,
    "purpose": purpose,
    "metadata": merged_metadata,
}

How agent messages are constructed

The SDK packs the agent's identity, instructions, context, and the user's prompt into a single user message with labelled sections:

text
Agent: code-reviewer

Instructions:
Review the following code.
Language: Python
...

Context:
Project: matilda-core

Code task:
def add(a, b): return a + b

build_agent_chat_request(...)

Low-level async builder that constructs the chat request dict without executing it. Useful for testing, logging, or custom execution paths.

Python
import asyncio

from matilda_agent_sdk import build_agent_chat_request


async def main():
    request = await build_agent_chat_request(
        prompt="Fix the failing test",
        agent={"name": "helper", "purpose": "code"},
        instructions="Be specific.",
        context="Project root: /repo",
        conversation_id="conv-1",
        file_ids=["file-1"],
        client_tools=[{"name": "read_file", "description": "Read a file", "parameters": {"type": "object"}}],
    )

    # request["messages"], request["responseMode"], request["conversation_id"], ...

Parameters

Fieldtypedescription
promptstrThe user's message. Required.
agentAgent | dict | NoneOptional agent to use. Defaults to a generic agent.
purposeAgentPurpose | NoneFallback purpose if agent doesn't specify one.
instructionsAgentInstructions | NoneOverride the agent's instructions.
contextstr | NoneOverride the agent's context.
metadatadict | NoneMerge with agent's metadata.
conversation_idstr | NoneAssociate with a conversation thread.
file_idslist[str] | NoneFile IDs to attach.
client_toolslist[dict] | NoneClient tools to advertise.
response_modestr | NoneOverride response mode.
response_schemastr | NoneRaw JSON Schema string to constrain output.
historylist[dict] | NonePrior-transcript messages replayed ahead of this turn.

Returns a dict shaped like a chat request (messages, responseMode, optional conversation_id / fileIds / clientTools / responseSchema).