← Back to Blog

Building Custom LangChain Tools for CI/CD Pipeline Interrogation

How I built a reliable WhatsApp AI shopping assistant for Clickmothercare that survives hallucinated products, silent save failures, and multi-agent handoff bugs.

Anas Rhimi
Anas Rhimi August 2026 • 8 min read

The Black Box of Failed Deployments

When a GitHub Actions pipeline fails in a complex microservices architecture, debugging often involves manually traversing dozens of logs, cross-referencing commit histories, and checking dependency graphs. Standard observability alerts tell you that a pipeline failed, but AI agents equipped with custom LangChain tools can tell you why and propose a fix. The challenge lies in giving the LLM precise, deterministic access to the CI/CD API without hallucinating commands.

Designing the Pipeline Interrogator Tool

To build an effective debugging agent, we must subclass LangChain's BaseTool. This tool will allow the agent to query the GitHub Actions API for specific run logs, parse the failure step, and extract the exact error traceback. We enforce strict typing using Pydantic to ensure the agent provides the correct repository and run ID.

from langchain.tools import BaseTool
from pydantic import BaseModel, Field
import requests
import re

class GitHubActionLogInput(BaseModel):
    repo: str = Field(..., description="The repository name in format owner/repo")
    run_id: int = Field(..., description="The unique ID of the failed GitHub Action run")

class GitHubActionLogTool(BaseTool):
    name = "fetch_failed_action_logs"
    description = "Fetches and extracts the error traceback from a failed GitHub Actions run."
    args_schema = GitHubActionLogInput
    github_token: str

    def _run(self, repo: str, run_id: int) -> str:
        # Line 1: Construct the API endpoint for workflow run logs
        url = f"https://api.github.com/repos/{repo}/actions/runs/{run_id}/logs"
        
        # Line 2: Set authorization and accept headers for zip download
        headers = {
            "Authorization": f"Bearer {self.github_token}",
            "Accept": "application/vnd.github+json"
        }
        
        # Line 3: Execute request, handling redirects for log archives
        response = requests.get(url, headers=headers, allow_redirects=True)
        if response.status_code != 200:
            return f"Error fetching logs: HTTP {response.status_code}"
            
        # Line 4: Extract the core error traceback using regex over the raw logs
        raw_logs = response.text
        error_pattern = re.compile(r'(?i)(error|exception|failed|traceback).*?(?=\n\n|\Z)', re.DOTALL)
        match = error_pattern.search(raw_logs)
        
        # Line 5: Return the isolated context to the agent, avoiding token limits
        return match.group(0) if match else "No explicit error traceback found in logs."

Code Annotation & Architectural Decisions

  • Line 1 & 2: We target the specific /logs endpoint. Passing the correct Accept header is critical here, as GitHub returns log archives that must be handled properly. Authentication is injected at instantiation via github_token, keeping secrets out of the prompt.
  • Line 3: allow_redirects=True is mandatory because GitHub's API frequently issues a 302 redirect to a temporary AWS S3 URL containing the actual log files. Failing to handle this returns a useless blank response to the agent.
  • Line 4: Raw CI/CD logs are massive, easily blowing past an LLM's context window (e.g., 100k+ tokens for heavy builds). Instead of returning the full payload, we use a compiled regular expression to isolate the traceback segment.
  • Line 5: We return only the dense, high-information chunk. If the agent receives a focused traceback (e.g., a specific Python ImportError or a Docker build failure), its reasoning engine can immediately propose a code fix without drowning in noise.

By coupling deterministic API interactions with regex-based context distillation, custom LangChain tools transform LLMs from passive chatbots into active CI/CD SREs capable of automated triaging.

Is your AI agent's infrastructure secure and reliable?

Book a Free 15-Min Technical Audit
Hire Me