Give a LangChain agent live AI answers as a tool
A LangChain agent can ask ChatGPT, Gemini, Perplexity, Copilot, Grok, Google AI Mode, Google Search and Google News while it reasons, by loading the tools from @answerline/langchain. createTools(client) returns one tool per engine plus tools for async tasks and the balance; each call returns structured JSON for the live answer, so the model reads the answer text and the URLs it cited rather than relying on its own training data. A Python package provides the same tools.
Why query the consumer products
A model API answers from its weights, plus whatever retrieval you wire in. The consumer products answer with their own live retrieval, ranking and presentation, and that output is what a buyer, journalist or customer reads. If an agent is doing competitive research, reputation checks or content planning, the question it needs answered is “what does ChatGPT show for this prompt in this market today”, and only a call to that surface answers it. Background: how AI engines choose citations and answer volatility.
The tools
A monitor tool is a LangChain tool that performs one synchronous POST /v1/monitor/<engine> request with the model’s arguments as the request body. Its input schema matches the API’s request schema, so the model sees the field names and constraints (prompt up to 2,000 characters, country as an ISO 3166-1 alpha-2 code, and so on).
| Tool | Task type | Route | Starter price per call |
|---|---|---|---|
monitor_chatgpt |
CHATGPT |
/v1/monitor/chatgpt |
$0.0006 |
monitor_perplexity |
PERPLEXITY |
/v1/monitor/perplexity |
$0.0015 |
monitor_gemini |
GEMINI |
/v1/monitor/gemini |
$0.0006 |
monitor_copilot |
COPILOT |
/v1/monitor/copilot |
$0.00274 |
monitor_grok |
GROK |
/v1/monitor/grok |
$0.00218 |
monitor_ai_mode |
AIMODE |
/v1/monitor/aimode |
$0.00206 |
monitor_google |
GOOGLE |
/v1/monitor/google |
$0.00103 for one page |
monitor_google_news |
GOOGLE_NEWS |
/v1/monitor/google/news |
$0.0002 for one page |
Three more tools cover the rest of the workflow:
create_async_task: submitsPOST /v1/async/taskwithtaskType,payload, and optionallypriority,idempotencyKeyandwebhook. Thepayloadis validated as the request of itstaskTypebefore anything is sent.get_task_status: readsGET /v1/async/task/{taskId}. Once a task isCOMPLETED, the engine result is atresponse.result.get_credits: readsGET /v1/credits, which returnsremaining, the balance in dollars.
Google options add to these prices (+$0.00071 per extra page and +$0.00071 for include.aioverview); ChatGPT’s include flags and extra Google News pages are free. See billing.
JavaScript setup
- Install. The integration needs Node 22.18 or later and declares
@answerline/sdk,@langchain/coreandzodas peer dependencies. AddlangchainforcreateAgent, and the provider package for your chat model.
npm install @answerline/sdk @answerline/langchain @langchain/core zod langchain
- Create a client and the tools, listing the engines the agent should have.
enginestakes task types and defaults to every engine; an unknown task type throws.
import { Client } from "@answerline/sdk";
import { createTools } from "@answerline/langchain";
import { createAgent } from "langchain";
const client = new Client({ apiKey: process.env.API_KEY!, baseUrl: "https://api.answerline.dev" });
const tools = createTools(client, { engines: ["CHATGPT", "GEMINI", "GOOGLE_NEWS"] });
- Build the agent. LangChain’s
createAgenttakes a model as a"provider:model"string or an initialized model instance, plustools(docs.langchain.com/oss/javascript/langchain/agents, checked 2026-09-17). Keep the model choice in configuration:
const agent = createAgent({ model: process.env.CHAT_MODEL!, tools });
const result = await agent.invoke({
messages: [
{
role: "user",
content:
"Ask ChatGPT and Gemini 'best payroll software for small teams' in the US. " +
"List the domains each cites, and check Google News for recent coverage of any brand both name.",
},
],
});
console.log(result.messages.at(-1)?.content);
- Check what it spent. Call
client.credits()before and after a run while you tune prompts. For the request above, the likely minimum ismonitor_chatgpt$0.0006 +monitor_gemini$0.0006 + onemonitor_google_news$0.0002 = $0.0014 at the Starter tier; every extra news query adds $0.0002.
A new account’s 100 free requests work on every engine; this run uses three of them, so they cover 33 runs.
Errors in JavaScript
When the API refuses a call, the tool throws an Error whose message includes the API’s response body, for example the code of INSUFFICIENT_CREDITS or a 400 validation failure with details[]. Timeouts and network failures are rethrown as <tool name> failed: <message>. Whether that thrown error reaches the model as a tool message or ends the run depends on your agent runtime’s tool-error handling, so decide it explicitly: surfacing the message lets the model fix a wrong argument, while ending the run is the safer default for INSUFFICIENT_CREDITS.
The client underneath is the TypeScript SDK, so its retry policy applies: 429 is retried with capped, jittered backoff, and a synchronous monitor call is retried only when the request never reached the server, because a 500 or 502 from a synchronous call is final and uncharged (API errors and retries).
Python setup
The Python integration has the same tools and a create_tools(client, engines=[...]) factory:
pip install answerline answerline_langchain
import os
from answerline import Client
from answerline_langchain import create_tools
client = Client(os.environ["API_KEY"], base_url="https://api.answerline.dev")
tools = create_tools(client, engines=["CHATGPT", "GOOGLE_NEWS"])
Client takes the API key as its first argument and base_url as a keyword. One behavior differs from JavaScript: in Python, API errors, timeouts and connection errors are returned to the model as the tool result instead of raised, so the agent reads the failure and can correct its arguments.
Writing your own tool
To avoid the dependency, or to trim responses before the model sees them, call the HTTP API from your own tool. A minimal ChatGPT tool with requests:
import os
import requests
from langchain_core.tools import tool
API = "https://api.answerline.dev"
HEADERS = {
"Authorization": f"Bearer {os.environ['API_KEY']}",
"Content-Type": "application/json",
}
@tool
def ask_chatgpt(prompt: str, country: str = "US") -> dict:
"""Ask ChatGPT a question in a country market and return its answer text and cited URLs."""
r = requests.post(f"{API}/v1/monitor/chatgpt", headers=HEADERS,
json={"prompt": prompt, "country": country}, timeout=330)
body = r.json()
if r.status_code != 200:
# Return the error to the model, as the packaged Python tools do.
return {"status": r.status_code, "error": body, "requestId": r.headers.get("X-Request-Id")}
result = body["result"]
return {
"text": result.get("text", ""),
"sources": [s["url"] for s in result.get("sources", [])],
"creditsCharged": r.headers.get("X-Credits-Charged"),
}
Pass [ask_chatgpt] to create_agent from langchain.agents, which accepts a "provider:model" string or a model instance (docs.langchain.com/oss/python/langchain/agents, checked 2026-09-17):
from langchain.agents import create_agent
agent = create_agent(model=os.environ["CHAT_MODEL"], tools=[ask_chatgpt])
agent.invoke({"messages": [{"role": "user", "content": "What does ChatGPT recommend for payroll software in the US?"}]})
Error bodies have two shapes: most are {"error": {"code", "message", "details", "timestamp"}}, but a validation failure on /v1/monitor/* is {"success": false, "error": "Request validation failed", "details": [...]} with status 400, so the tool hands the whole body to the model rather than picking one field. The 330-second timeout sits above the five minutes a synchronous call can take. Unlike the SDK, this tool does not retry; add a retry on 429 if the agent runs calls in parallel.
What the model reads back
Every engine returns answer text and sources[]; the rest varies by engine, and a prompt that assumes otherwise leads the model to report fields that are not there:
| Field | Where it exists |
|---|---|
entities[] (brand and product names) |
ChatGPT only |
citationPills[] |
ChatGPT (0-based citationPillId), Perplexity, Gemini, AI Mode (1-based), Copilot; not Grok |
searchQueries[] |
ChatGPT (free with include.searchQueries; empty on the answers served today), Grok; coming on Copilot |
related_queries[] |
Perplexity |
organicResults[], peopleAlsoAsk[], aioverview |
Google Search |
newsResults[] |
Google News |
Optional arrays are omitted when empty, so a tool or prompt should treat a missing shoppingCards as “none shown”, not as an error. For deeper field coverage see the engine pages for ChatGPT, Gemini and Google News.
Sync tools or async tasks
Monitor tools block the agent until the answer arrives and hold the connection while they run. That suits a question the agent needs answered now, not a sweep.
| Situation | Use | Why |
|---|---|---|
| One to a few lookups inside a reasoning step | monitor_* |
The agent needs the answer to continue |
| Many prompts, or prompts across many markets | create_async_task, then get_task_status |
Tasks queue (about 100,000 typical tasks per account) |
| Results should land in a store, not in the conversation | create_async_task with webhook |
The finished task is delivered to your endpoint |
| A scheduled, fixed prompt set | A batch outside the agent | POST /v1/async/task/batch takes 1 to 500 tasks per request |
Worked example: an agent checks 40 prompts on ChatGPT, 40 × $0.0006 = $0.024 at the Starter tier. As monitor calls, the agent waits on each. As async tasks, they queue and run in the background. A task must start within 4 hours of creation and finish within five minutes of its first start, and an unfinished or failed task is not charged. Tell the agent to set an idempotencyKey derived from the prompt, market and day, so a retried step cannot queue the same work twice (idempotency keys).
Guardrails for production agents
- Narrow
engines. Every tool’s schema goes into the model’s context. An agent that only needs ChatGPT and Google News should not see eight monitor tools. - Set a dollar budget in the system prompt, and enforce it in code. Ask the agent to call
get_creditsbefore large steps, and checkclient.credits()in your own loop, stopping below a threshold. - Mind runaway loops. There is no per-account request rate limit and no concurrency cap (rate limits), so only the balance stops a loop. A runaway loop can drain it quickly, which is what the budget check is for.
- Pin markets. Put
countryinto the task description; the answer for the same prompt differs by market. ChatGPT and Gemini answer from 213 countries, and Gemini also takes a US state (state-level answers). - Keep payloads lean. Leave
include.rawResponseoff unless the agent needs it; it adds context, and on ChatGPT it adds to the price.
Without LangChain
The same engines are available to any MCP client through the hosted MCP server at https://mcp.answerline.dev/mcp, which exposes ChatGPT, Gemini, Perplexity, Copilot, Grok, AI Mode, Google Search, Google News and credits tools but not the async task tools (MCP integration, giving an agent live answer data). For every integration option, see /integrations/langchain and /docs/integrations.
Engine request and response fields: /engines/chatgpt.
Questions
Which tools does the LangChain integration create?
One synchronous monitor tool per engine: monitor_chatgpt, monitor_perplexity, monitor_gemini, monitor_copilot, monitor_grok, monitor_ai_mode, monitor_google and monitor_google_news, plus create_async_task, get_task_status and get_credits. The engines option narrows the monitor tools to the task types you pass.
Is there a Python version?
Yes. The answerline_langchain package provides the same tools through create_tools(client, engines=[...]). You can also call the HTTP API directly from your own LangChain tool.
How do I stop an agent spending without limits?
Narrow engines so the model sees only the tools it needs, set a dollar budget in the system prompt and enforce it in your own loop by checking the balance before large steps, and leave rawResponse off unless the agent uses it.
What happens when the API returns an error inside a tool?
In JavaScript the tool throws an Error whose message includes the API's response body. In Python the error is returned to the model as the tool result, so the agent can read the code and adjust.
Do all engines return entities?
No. entities[] exists only on ChatGPT, where it lists the brands and products the answer names; on other engines, find brand names in the answer text. Every engine returns answer text and sources, but other fields such as citation pills, search queries and shopping cards vary by engine.