Skip to content

Agents

Philosophy

We treat agents a little differently. Traditionally, the agent concept has been tightly coupled with a single LLM: one agent equals one LLM.

In contrast, parallem associates an agent with a program. What does this mean?

An agent is just a python function

The most idiomatic way to declare an agent is with a vanilla Python function that takes a pllm.AgentContext. For example:

import parallem as pllm
from dotenv import load_dotenv


def haiku_writer_agent(agt: pllm.AgentContext):
    # Declare the agent.
    conv = agt.get_msg_state()
    conv.ask_llm("Please name an animal in 1 word.")
    conv.ask_llm(f"Write a haiku about {conv[-1].final_answer}(s).")
    return conv[-1].final_answer


if __name__ == "__main__":
    load_dotenv()

    with pllm.resume_directory(
        ".pllm/simplest",
        provider="openai",
        strategy="sync",
        dashboard=True,
        hash_by=["llm"],
    ) as orch:
        # Instantiate the agent.
        with orch.agent("Yosa Buson") as agt:
            agt.print(haiku_writer_agent(agt))

        with orch.agent("Matsuo Basho") as agt:
            agt.print(haiku_writer_agent(agt))

But it is just a python function! Nothing is stopping you from embedding all the logic directly in the main function, as is the case in simplest.py. The choice is up to you.

More parameters

You can always pass more parameters to the agent:

def haiku_agent(agt: pllm.AgentContext, subject: str):
    resp = agt.ask_llm(f"Please write a haiku about {subject}.")
    return resp.final_answer

An agent is more than an LLM

After decoupling the agent concept from one single LLM, it becomes natural to express concepts including:

  1. One agent calling multiple LLMs (dynamic model switching, see simplest_multi.py):

    def polyglot_agent(agt: pllm.AgentContext):
        resp = agt.ask_llm("What is your model name?", llm="gpt-5-nano")
        resp2 = agt.ask_llm("What is your model name?", llm="gemini-2.5-flash")
    

  2. Non-linear, branching conversations

    def animal_agent(agt: pllm.AgentContext):
        resp = agt.ask_llm("Please simply name 8 animals in a comma-separated list.")
        for animal in resp.split(","):
            resp2 = agt.ask_llm(f"Please write a paragraph on the {animal.final_answer.strip()}.")
    

  3. Asking non-LLMs

    def best_animal_agent(agt: pllm.AgentContext):
        resp = agt.ask_llm("What is the best animal?")
        resp2 = agt.ask_human(f"Would you agree that {resp.final_answer} is the best animal?")
    

The Ask API epitomizes the philosophy that the agent is not an LLM, but a program.

See also