Whatever message this page gives is out now! Go check it out!

Agent

Last update:
May 18, 2026
The Agent() function in ColdFusion turns a stateless LLMModel (created with ChatModel()) into a full conversational AI agent. It adds test:
  • Multi-turn conversation management: Handles ongoing dialogue across multiple prompts.
  • Memory: Remembers previous messages, either globally or per user, with optional persistence via Redis, Memcache, or a custom store.
  • System personas: Set instructions or roles for the AI, such as "You are a travel assistant.", applied persistently across all subsequent calls.
  • Function/tool calling: Lets the AI invoke ColdFusion CFC methods or external MCP clients to perform tasks, fetch data, or trigger actions. (Configuration details are in Deploy and administer.)

How does it work?

Pass a configuration struct to Agent(), which must include a CHATMODEL (the LLMModel instance). Optionally add:
  • TOOLS: A list of ColdFusion CFCs or MCP clients the AI can use. See Deploy and administer.
  • CHATMEMORY: Configuration for how conversation history is stored and managed.
The result is an Agent instance with the following methods:
MethodDescription
agent.chat(message)Sends a message using the current persona and memory context.
agent.chat(message, userId)Same as above, but memory is scoped to a specific user/session.
agent.chat(chatRequestStruct)Allows explicit system and user messages for that call.
agent.chat(chatRequestStruct, userId)Combines rich request struct with per-user memory.
agent.systemMessage(messageString)Sets a persistent system-level instruction for all subsequent calls.
Note:
Note: agent.systemMessage() requires CHATMEMORY to be configured to work correctly. If CHATMEMORY is not set, the system message is accepted without error but not sent to the provider. Use a chatRequest struct as an alternative when not using memory.

Agent example


<cfscript>
    // 1. Create the LLMModel
    chatModel = ChatModel({
        PROVIDER  : "openAi",
        APIKEY    : "#application.apiKey#",
        MODELNAME : "gpt-4o-mini"
    });

    // 2. Create an Agent
    agent = Agent({ CHATMODEL : chatModel });

    // Method 1: Plain string
    response1 = agent.chat("What is the capital of Japan?");
    writeOutput(response1.message);

    // Method 2: chatRequest struct (overrides persona for this call)
    chatRequest = {
        SYSTEMMESSAGE : "You are a poet. Reply in 2 lines.",
        USERMESSAGE   : { MESSAGE : "Tell me about the ocean." }
    };
    response2 = agent.chat(chatRequest);
    writeOutput(response2.message);

    // Method 3: Persistent system message
    agent.systemMessage("You are a scientist. Be concise.");
    response3 = agent.chat("Why is the sky blue?");
    writeOutput(response3.message);

    // Method 4: Override system message for one call
    chatRequest2 = {
        SYSTEMMESSAGE : "You are a pirate. Use pirate speak.",
        USERMESSAGE   : { MESSAGE : "What is gravity?" }
    };
    response4 = agent.chat(chatRequest2);
    writeOutput(response4.message);
</cfscript>
            

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page