Add an AI agent
Goal
Create an ai_agents row plus a prompt_configs row, then run it either conversationally (ai-agents?action=run on a thread) or inside a pipeline (agent_inference stage). See AI agents, prompt configs, and embeddings.
Steps
- Create the prompt config. Admin UI → Configs → Prompts → New, or
POST /api/prompt-configs. Key fields:system_prompt,context_template(substitutes{{user_message}}and{{conversation_history}}only),model(defaultgpt-4o),temperature(default0.7),max_tokens(default4000),is_multi_turn,max_history_messages(default20) (.framework/functions/prompt-configs.ts). - Create the agent. Admin UI → Configs → AI Agents → New, or
POST /api/ai-agents—nameandagent_typerequired; optionalmodel_config,system_prompt,tools,capabilities,constraints,metadata(.framework/functions/ai-agents.ts). Link the prompt config viametadata.default_prompt_config_id. - Configure the LLM integration. Inference routes through
integration-outboundagainst the integration slugopenaiwithcredential_field: 'api_token'(.framework/functions/_shared/llm.ts). Migration 004 seedsopenaiwithapi_token: { source: 'env_var', value: 'OPENAI_API_KEY' }— set that env var or update the credential. Without it the runner returns a[Mock Response]string instead of calling the model (.framework/functions/_shared/agent-runner.ts). - Conversational path —
ai-agents?action=run.POST /api/ai-agents?action=runwith{ thread_id, user_message, mode };modeisstep|complete(default) |episode. The runner resolves the agent fromthread.data.agent_id→design_schema.default_agent_id, and the prompt config fromthread.data.prompt_config_id→agent.metadata.default_prompt_config_id— both must beis_active(.framework/functions/_shared/agent-runner.ts). A prompt-injection preflight readsconfig.security.prompt_injection(off|flag|block) on the agent (.framework/functions/ai-agents.ts). - Pipeline path —
agent_inferencestage. Add a stage withstage_type: 'agent_inference'andconfig.context(required;{{trigger.*}}/{{record.*}}templates resolve). With aconfig.webhook_urlthe stage POSTs{ context, model, temperature, max_tokens }to that URL via thegeneric-httpintegration; without one it returns a mock response —[Mock Response] Received N chars of context(.framework/functions/_shared/pipeline-runner.ts).
Verify
POST /api/ai-agents?action=runon a thread whosedata.agent_idpoints at the agent — returns a completion (or the mock string when the OpenAI credential is missing).agent_steprate-limit debits and injection audits appear inpublic.logs.- The
agent_inferencestage output appears in the execution's stage results (Observability → Executions).
Common mistakes
threads.target_typeisNOT NULL. A chat thread must be bound to a target record — there is no unbound "conversation" row. Point it at something stable such astarget_type: 'account'+target_id: <account id>and keepdata.agent_id/data.prompt_config_idon the thread.- No
default_prompt_config_idon the agent and noprompt_config_idon the thread — the run has no prompt config to assemble from; always set the agent fallback. - Missing
OPENAI_API_KEY/ unconfiguredopenaiintegration — you get a mock response, not an error; check the reply text for[Mock Response]. agent_inferencewith nowebhook_url— returns a mock. This stage is an HTTP bridge, not the in-processrunAgentpath.- Agent or prompt config
is_active: false— resolution fails before inference. context_templatewith unsupported placeholders — only{{user_message}}and{{conversation_history}}substitute; anything else stays literal (.framework/functions/_shared/agent-runner.ts).