Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

AI agent system

Introduction#

LangGraph ReAct loop: answer directly or call tools until done. Tools cover search, websites, YouTube, Gmail, Calendar, JIIT attendance, and browser actions. Providers plug in through one LLM layer; a sanitizer checks browser plans.

Project structure#

The repository organizes functionality by concerns:

  • agents: Reactive agent graph and tool integration
  • core: LLM provider abstraction and configuration
  • prompts: Domain-specific prompts and injection validation
  • tools: Tool implementations and schemas
  • services: Business logic and orchestration
  • routers: API endpoints
  • models: Request/response schemas
  • utils: Utilities such as sanitization
  • extension: Browser extension entrypoints and utilities

Core components#

  • Reactive Agent Graph: Defines a LangGraph workflow with an agent node and a tool execution node, enabling conditional routing between reasoning and tool use.
  • Tool Integration: Structured tools with typed schemas, composition based on context (tokens, session payloads), and async execution.
  • LLM Provider Abstraction: Centralizes provider selection, model mapping, environment-driven configuration, and runtime client instantiation.
  • Prompt Engineering: Supplies domain-specific prompts for browser automation and injection validation for safety.
  • State Management: Maintains conversation context and message sequences for multi-turn interactions.
  • Orchestration: Bridges the extension UI to backend APIs and services, capturing page context and assembling tool-ready payloads.

Architecture overview#

Stack from the outside in:

  • Extension layer captures user intent, active tab context, and constructs payloads for backend endpoints.
  • API layer exposes endpoints for agent orchestration and tool-specific operations.
  • Service layer translates requests into agent state, injects context, and invokes the compiled LangGraph.
  • Agent layer executes the reasoning loop, optionally invoking tools and returning final responses.
  • Tool layer encapsulates domain capabilities with validated inputs and safe execution patterns.
  • LLM abstraction provides a unified interface across providers and models.

Detailed component analysis#

Reactive agent graph and state management#

The agent uses a LangGraph StateGraph with two nodes:

  • Agent node: binds available tools to the LLM and generates a response.
  • ToolNode: executes selected tools and returns results as ToolMessages. Conditional edges route from agent to tool execution when tool_calls are detected, then back to the agent until completion.

Key implementation highlights:

  • AgentState defines a messages accumulator.
  • Payload conversion helpers normalize content and preserve tool_calls and tool_call_id.
  • GraphBuilder compiles the workflow once and caches it.
  • run_react_agent converts external payloads to LangChain messages, invokes the graph, and returns normalized payloads.

Tool integration system#

The tool system:

  • Defines structured tools with Pydantic schemas for inputs.
  • Dynamically composes tools from a base set, augmenting with Google and JIIT capabilities when context is provided.
  • Executes tool coroutines asynchronously and formats results safely.

Tool selection and execution patterns:

  • Tool selection is driven by the LLM’s tool_calls; LangGraph routes to ToolNode automatically.
  • Execution runs in threads to keep LLM calls responsive.
  • Results are normalized to text or JSON for downstream consumption.

LLM provider abstraction and model-agnostic design#

The LLM abstraction centralizes provider configuration and client instantiation:

  • Provider configs map provider names to LangChain chat model classes, default models, and environment variables.
  • LargeLanguageModel initializes clients with environment-driven parameters and validates presence of required keys/base URLs.
  • The agent uses a cached client instance so model selection stays consistent.

Provider configuration and environment handling:

  • API keys and base URLs are resolved from environment variables or provided parameters.
  • Default models are provided per provider; errors guide users to configure missing values.

Prompt engineering system and injection validation#

Domain-specific prompts:

  • Browser automation prompt defines actions, constraints, and JSON output expectations for generating action plans the sanitizer can check.
  • Other prompts support specialized tasks (e.g., website, YouTube, GitHub).

Injection validation:

  • A validator prompt determines whether provided markdown text contains prompt injection attempts, returning a boolean decision.

Agent state management and conversation context#

The service layer:

  • Builds AgentState from incoming chat history and question.
  • Injects page context as a SystemMessage when client HTML is available.
  • Logs message sequences for observability and invokes the graph.

Multi-Step workflow orchestration#

The workflow:

  • Starts with a user question and optional chat history.
  • Optionally augments with page context and tool availability.
  • Iteratively decides between reasoning and tool execution until a final answer is produced.

Browser action generation and safety#

The browser action tool:

  • Accepts a goal, optional target URL, DOM structure, and constraints.
  • Delegates to a service that builds a chain using the browser automation prompt and LLM client.
  • Returns a JSON action plan.

Safety validation:

  • A sanitizer checks JSON structure, validates action types, and enforces required fields.
  • Additional checks prevent dangerous script patterns.

Extension integration patterns#

The extension:

  • Parses slash commands and maps them to backend endpoints.
  • Captures active tab HTML and constructs payloads for agent endpoints.
  • Supports file uploads and special URL normalization for GitHub repositories.
  • Uses a WebSocket client to coordinate agent execution and progress.

Dependency analysis#

The system exhibits low coupling and high cohesion:

  • agents/react_agent.py depends on core/llm.py and agents/react_tools.py.
  • agents/react_tools.py depends on prompts and tool implementations.
  • services/react_agent_service.py orchestrates agents and tools.
  • routers/react_agent.py delegates to services.
  • Extension utilities depend on agent-map and websocket-client for coordination.

Performance considerations#

  • Async execution: Tools run in threads to avoid blocking the LLM invocation loop.
  • Caching: GraphBuilder caches the compiled graph to reduce overhead on repeated invocations.
  • Provider selection: Choose appropriate models and providers based on latency and cost profiles.
  • Payload sizing: Limit DOM structures and chat histories to reasonable sizes to keep prompt costs and latency manageable.
  • Rate limits: Respect provider rate limits and implement retries with backoff.

Troubleshooting guide#

Common issues and remedies:

  • Missing API keys or base URLs: Ensure environment variables are set for the chosen provider.
  • Tool failures: Inspect tool coroutines for exceptions and validate inputs using Pydantic schemas.
  • JSON action plan errors: Use the sanitizer to identify malformed or unsafe actions.
  • Extension errors: Verify endpoint resolution and payload construction; check WebSocket connectivity.
  • Injection risks: Apply the injection validator to incoming markdown content before processing.

Conclusion#

Typed tools, conversation state, and plan validation are what make multi-step runs trustworthy. Log service and router traffic when a tool call looks wrong.

Appendices#

Agent configuration examples#

  • Configure provider and model via environment variables or constructor parameters.
  • Supply optional context (Google access token, JIIT session payload) to dynamically compose tools.
  • Use the extension slash command to trigger workflows and capture page context.

Tool creation checklist#

  • Define a Pydantic input schema with constraints.
  • Implement an async coroutine that performs the operation safely.
  • Wrap as a StructuredTool with a clear description.
  • Register the tool in the tool builder and ensure it is included in the agent graph.

Debugging techniques#

  • Enable logging at the service and router layers to trace message sequences.
  • Inspect tool call payloads and results for mismatches.
  • Validate browser action plans with the sanitizer and review warnings.
  • Monitor provider quotas and adjust model selection accordingly.