Agents

How to create an AI agent from scratch: a step-by-step guide

Build an AI agent step by step: define the job, choose a model, add tools, knowledge, memory and guardrails, then test and deploy. With working Python code.

Oleksii BabychMachine learning engineer8 min readUpdated

In short

An AI agent is a language model running in a loop that can call tools, read your data and remember context until a task is done. To build one, define a narrow job and how you will measure it, pick a model, write instructions, add tools, knowledge and memory, put guardrails and approvals on risky steps, then test on real cases before you deploy and monitor it.

To create an AI agent from scratch, you connect a language model to tools and data, give it clear instructions, and run it in a loop where it reasons, acts and checks the result until the task is done. What makes an agent reliable is everything around that loop: a narrow job, well-described tools, grounded knowledge, memory, guardrails and tests on real cases.

This guide explains the components, walks through eight build steps, and ends with a working Python example of a bank card support agent that looks up data, remembers the conversation and asks a person before it blocks a card.

What is an AI agent?

An AI agent is software that uses a language model to decide what to do next, takes actions through tools such as APIs and databases, observes the results and repeats until it reaches its goal. The model supplies judgment; the tools let it act; the loop lets it recover from a wrong step.

It helps to compare agents with the two things they are often confused with:

Chatbot Workflow automation AI agent
How it decides Answers each message Follows fixed rules Plans steps toward a goal
Can it act in systems? Rarely Yes, along a fixed path Yes, choosing which tool to use
Handles the unexpected Poorly Fails or stops Adapts, or escalates to a person
Best for Simple questions Stable, repetitive processes Varied tasks that need judgment

Most production systems combine them: a workflow with fixed checkpoints, and agents doing the steps that need judgment. Our guide to LLM agents covers the concepts in more depth.

What are the core components of an AI agent?

Component What it does Example in a card support agent
Model Reasons and chooses the next step A hosted or self-hosted chat model
Instructions Set the scope, rules, tone and when to escalate "Never ask for a full card number or PIN"
Tools Let the agent look things up and take actions Card status lookup, block card
Knowledge Documents the agent retrieves and cites Fee schedule and card policy
Memory Keeps context within and across conversations What the customer said two messages ago
Guardrails Check inputs, outputs and actions Detect personal data, approve before blocking
Runtime and tracing Runs the loop and records every step A trace of each lookup, with cost and latency

Should you build from scratch, use a framework, or use a platform?

"From scratch" can mean three different things, and the right one depends on your team:

Approach What you write Best for Watch out for
Raw model API The loop, tool calling, retries, memory and logging Learning how agents work Everything production needs is yours to build
Open-source SDK Agent configuration, tools and business logic Engineering teams that want control in code You still host, secure and monitor it
Platform Configuration on a canvas, code where needed Mixed teams and regulated production Check self-hosting options and lock-in

Dynamiq covers the last two with one engine: the open-source Python SDK (Apache-2.0) and a visual builder that run the same agents, so a team can start in code and hand over to a canvas, or the other way around.

How do you build an AI agent? Eight steps

Step 1: Define one job and how you will measure it

Start with a single, narrow task that happens often and has a clear definition of done, such as "answer card questions and block cards on request" rather than "help customers". Write down what success looks like before you build: resolution rate, accuracy on a set of real cases, time to answer, and which requests must go to a person.

Step 2: Choose the model

Pick the smallest model that handles your task reliably, and test two or three candidates on the same cases. Consider where the model runs: hosted models are easiest to start with, while open-weight models such as Llama, Mistral or Qwen can run inside your own environment when data cannot leave it. Keep the model swappable; it will change more often than anything else in your agent.

Step 3: Write the instructions

The instructions, often called the role or system prompt, define the agent's scope, rules and style. Be specific about what it must never do and when it must hand off. Vague instructions produce vague agents.

Step 4: Give it tools

Tools are how an agent acts: API calls, database queries, search, code execution, or other agents. Each tool needs a clear name, a description the model can act on and a typed input schema. Start with read-only tools and add actions once you trust the agent. Many tools can now be connected through the Model Context Protocol, an open standard for exposing tools to agents.

Step 5: Ground it in your knowledge

For questions about your products, policies or documents, give the agent retrieval over a knowledge base instead of relying on what the model learned in training. Retrieval-augmented generation (RAG) returns relevant passages the agent can quote and cite. In regulated settings, retrieval should respect permissions, so the agent only reads what the person it serves is allowed to see. See how knowledge bases handle parsing, retrieval and permissions.

Step 6: Add memory

Session memory keeps the conversation coherent, so a customer does not repeat themselves. Long-term memory stores durable facts about a user, such as a preferred language, and recalls them in later sessions. Scope both to the user and session, and decide what should never be stored.

Step 7: Add guardrails and approvals

Screen inputs for personal data and prompt injection before the model sees them, validate outputs against the format and policy you expect, and require a person's approval before any step that is hard to undo, such as moving money, changing an account or sending a letter. Our guardrails page covers the detectors and approval gates in detail.

Step 8: Test, deploy and monitor

Build a test set from real cases, including edge cases and attempts to misuse the agent, and run it on every change to instructions, tools or models. After launch, trace every run, watch cost and latency, and score a sample of live conversations so quality problems surface before customers report them. Deploy with versions, so you can roll back a bad change in one step.

Build it in Python: a working example

This example uses the open-source Dynamiq SDK (pip install dynamiq) and an OpenAI model. It builds a card support agent with two tools, conversation memory and an approval step before a card is blocked. The card data is a stand-in for your core banking API.

from dynamiq import Workflow
from dynamiq.connections import OpenAI as OpenAIConnection
from dynamiq.flows import Flow
from dynamiq.memory import Memory
from dynamiq.memory.backends import InMemory
from dynamiq.nodes.agents import Agent
from dynamiq.nodes.llms import OpenAI
from dynamiq.nodes.tools.function_tool import function_tool
from dynamiq.nodes.types import InferenceMode
from dynamiq.types.feedback import ApprovalConfig, FeedbackMethod

# A stand-in for your core banking API
CARDS = {"4821": {"status": "active", "last_transaction": "Online store, 214.90 USD"}}


@function_tool
def get_card_status(card_last4: str, **kwargs) -> dict:
    """Look up a card's status and last transaction by its last four digits."""
    return CARDS.get(card_last4, {"error": "Card not found"})


@function_tool
def block_card(card_last4: str, reason: str, **kwargs) -> str:
    """Block a card. Use only after the customer has confirmed they want it blocked."""
    CARDS[card_last4]["status"] = "blocked"
    return f"Card ending {card_last4} is now blocked."


# Every block waits for a person to approve it
block_card_tool = block_card(
    approval=ApprovalConfig(
        enabled=True,
        feedback_method=FeedbackMethod.CONSOLE,
        msg_template=(
            "Block card ending {{input_data.card_last4}} "
            "(reason: {{input_data.reason}})? "
            "Press Enter to approve, or type feedback to cancel."
        ),
    )
)

llm = OpenAI(connection=OpenAIConnection(), model="gpt-4.1", temperature=0.1)

agent = Agent(
    name="card-support",
    id="card-support",
    llm=llm,
    role=(
        "You help bank customers with card questions.\n"
        "- Look up the card with get_card_status before you answer.\n"
        "- Block a card only when the customer asks for it and confirms.\n"
        "- Never ask for a full card number, PIN or password.\n"
        "- For disputes and anything else, say a colleague will follow up."
    ),
    tools=[get_card_status(), block_card_tool],
    memory=Memory(backend=InMemory()),
    inference_mode=InferenceMode.XML,
    max_loops=8,
)

workflow = Workflow(flow=Flow(nodes=[agent]))
session = {"user_id": "customer-17", "session_id": "chat-001"}

for message in [
    "I don't recognize the last payment on my card ending 4821.",
    "That wasn't me. Please block the card.",
]:
    result = workflow.run(input_data={"input": message, **session})
    print(result.output[agent.id]["output"]["content"])

What each part does:

  • @function_tool turns a Python function into a tool. The type hints become the input schema and the docstring becomes the description the model reads.
  • ApprovalConfig pauses the block_card tool until a person approves in the console. If they type feedback instead, the step is canceled and the agent sees the feedback.
  • Memory with user_id and session_id lets the second message refer to "the card" without repeating the number.
  • InferenceMode.XML makes the model format its reasoning and tool calls as XML, which works reliably across providers.

To swap the model, change the LLM class, for example to Anthropic or TogetherAI; the SDK supports 29 model providers. The SDK documentation covers knowledge retrieval, long-term memory, sandboxes and multi-agent orchestration.

How do you build the same agent without code?

In Dynamiq's Agent Builder, the same agent is a set of nodes on a visual canvas: an agent node with its model, instructions, tools and memory, wired between an input and an output. You can describe the workflow in a prompt and get a first draft to edit, add Choice nodes for rules, and turn on approval for any tool step, with fields a reviewer can edit before it runs.

When it works, deploy it as an App with its own API, a chat widget or a schedule, with versions and rollback. Guardrails that detect PII and prompt injection, evaluations and traces with cost and latency are part of the platform, which runs in Dynamiq Cloud or self-hosted in your own cloud or data center.

If you want an agent that works today without designing one, AI Coworker is a ready-made agent with its own cloud sandbox, connectors to your apps and scheduled tasks, in the browser, Slack, Microsoft Teams and Telegram.

Where do AI agents deliver value first?

  • Customer support: resolving routine requests and handing complex ones to a person with context. An Asian neo-bank automated about 85% of support inquiries this way and went live in 30 days.
  • Back-office operations: reconciliations, exception handling and report drafting, with approvals before anything posts.
  • Document review: extracting and checking fields in loan files, claims and contracts.
  • Compliance: preparing KYC and AML cases for analysts to decide.
  • IT and HR help desks: answering policy questions and handling routine requests such as access and onboarding tasks.

FAQ

How do I create an AI agent from scratch?

Define one narrow job and how to measure it, choose a model, write instructions, add tools for the actions and lookups the job needs, ground the agent in your knowledge, add memory, put guardrails and approvals on risky steps, and test it on real cases before you deploy and monitor it.

Do I need to train a model to build an AI agent?

No. Most agents use a pre-trained model and get their behavior from instructions, tools and retrieved knowledge. Fine-tuning is optional and usually reserved for narrow needs such as a specific output format or domain vocabulary.

What programming language is best for building AI agents?

Python is the most common choice because most agent SDKs, model clients and data libraries support it first. The Dynamiq SDK is a Python library, and agents built with it also run on the visual platform.

How long does it take to build an AI agent?

A prototype comes together quickly. Most of the time goes into connecting real systems, testing on real cases and passing security review. With an experienced team and a platform, a production agent can go live in weeks; the neo-bank above went live in 30 days.

Can I build an AI agent without coding?

Yes. Visual builders let you configure the model, instructions, tools, memory and approvals on a canvas, and generate a first draft from a prompt. Keep a code path available for custom tools and integrations.

Put this into practice.

See an agent built for your workflow, running in your environment, with our engineers.