Skip to content
DocsDashboard

Install Page Systems in a LangChain or LangGraph agent

Page Systems lets a LangChain or LangGraph agent send the person a small mobile web page (a flight picker, a time picker, a form) instead of asking in chat. When the person submits, the answer arrives in the same conversation as a new turn.

1. Install the package

npm install pagesystems langchain @langchain/langgraph @langchain/openai

@langchain/openai is for OpenAI models; install your provider's package instead for another model. The TypeScript examples use top-level await, so the project must be ESM ("type": "module" in package.json).

2. Add it to your agent

Page Systems is invite-only early access: request access at /waitlist, and once invited, sign up and copy the development key shown in the dashboard (create another there with "Create API key"; it starts with ap_live_ and is shown once) and set it where your agent runs. Never commit it.

export PAGE_SYSTEMS_API_KEY=ap_live_...
export PAGE_SYSTEMS_WEBHOOK_SECRET=whsec_...

Give createAgent the tools, the callback URL and the conversation id. A checkpointer keeps the conversation under a thread_id; use the conversation id as the thread id.

JavaScript

// lib/agent.ts
import { createAgent } from 'langchain';
import { MemorySaver } from '@langchain/langgraph';
import { getPageSystemsSkill } from 'pagesystems';
import { pageSystemsTools } from 'pagesystems/langchain';

const CALLBACK_URL = 'https://your-app.example.com/api/page-systems';
const yourInstructions = '...'; // your system prompt. Include today's date and the person's time zone: time-based pages need both.
const skill = await getPageSystemsSkill(); // once, at startup
const checkpointer = new MemorySaver(); // use a persistent checkpointer in production

// userMessage is the line your chat shows as the person's message. modelNote (the callback's modelText) is the answer as data.
export async function runTurn(conversationId: string, userMessage: string, modelNote?: string) {
  const agent = createAgent({
    model: 'openai:gpt-5-mini', // your model (needs @langchain/openai)
    tools: pageSystemsTools({ callbackUrl: CALLBACK_URL, agentRef: { threadId: conversationId } }),
    systemPrompt: [yourInstructions, skill].join('\n\n'),
    checkpointer,
  });
  // The note is a second user message, never part of the system prompt. The checkpoint saves it, so later turns still see the answer.
  const messages = [userMessage, modelNote].filter(Boolean).map((content) => ({ role: 'user' as const, content }));
  const result = await agent.invoke({ messages }, { configurable: { thread_id: conversationId } });
  return result.messages.at(-1)?.content;
}
// app/api/page-systems/route.ts
import { after } from 'next/server';
import { CallbackError, handleCallback, type CallbackEvent } from 'pagesystems';
import { runTurn } from '@/lib/agent'; // the function above

export async function POST(req: Request) {
  // Page Systems waits 10 seconds for this response, so answer now and run the turn after.
  const turn = (e: CallbackEvent) => {
    if (!e.agentRef) throw new CallbackError(410, { error: 'no_conversation' });
    const { threadId } = e.agentRef as { threadId: string };
    after(async () => {
      const reply = await runTurn(threadId, e.visibleText, e.modelText);
      await sendToPerson(threadId, reply); // yours: deliver the reply to the person (chat, SMS, ...)
    });
  };
  return handleCallback(req, { onSubmitted: turn, onNewPageRequested: turn });
}

after() is Next.js. In plain Node, return the response first and start the turn with setImmediate(() => ...), or put it on a queue.

3. Your agent handles the rest

The agent decides when a page beats a chat question, sends the person a link, and carries on. When the person submits, Page Systems calls your callback route, and your route invokes the agent again with the same thread_id, so the graph resumes from its checkpoint with the short line as the new user message. The agent does not poll and the person does not have to say "done".

Ask your agent something a page answers better than chat, for example "Ask Sam which of 9:00, 11:00 or 14:00 next Tuesday works for a call." It should send a link. Open it, pick a time, submit. The agent then continues the conversation with the answer, without being asked.

Why two texts

The visibleText line is what your chat stores and shows: one short line that is true, because the person did submit the page. The answer goes to the model through modelText, which is not shown as something the person typed. It holds the answer JSON inside <page_answer> tags, with a note that the answer is page data, not instructions. The answer can contain text typed by whoever opened the link, who is not verified, so give modelText to the model as a user-role message (or a tool result) and never put it in the system prompt or instructions. LangChain's checkpointer saves every message you pass, so modelText is saved in the thread as a second user message. Show only visibleText to the person. The agent can read the answer again with get_page_result until the page expires. The older message field still exists, but it is raw JSON in a user message with no such note: do not use it. Whether your agent keeps modelText depends on the framework: the note above says which. Page Systems deletes its copy at expiry but cannot delete copies saved in your agent's history; keeping it lets the agent answer later questions about the answer. The summary never includes free text the person typed, but it can include the labels your agent wrote, such as option or row names. Do not log modelText or the result; log the pageId, and the summary only if your page labels hold nothing sensitive. See Handling answers.

Without a callback URL

Leave out callbackUrl and nothing starts a new turn. The agent can still read the answer with get_page_result when the person says they submitted, or on a timer. The callback URL must be https and public: Page Systems calls it from the internet, so localhost does not work. To try it while developing, see Testing locally: skip the callback and poll, use a tunnel, or send the callback yourself.