9.5 C
Canberra
Thursday, October 23, 2025

A Information to Constructing a Self-Documenting AI


Usually, we spend a big period of time making an attempt to grasp blocks of code, comprehend the parameters, and decipher different complicated points of the code. I believed to myself, can the much-hyped AI Brokers assist me on this regard? The system that I have been to create had a transparent goal: to offer useful feedback, flags on duplicate variables, and, extra importantly, take a look at capabilities to see the pattern outputs myself. This seems very a lot doable, proper? Let’s design this Agentic system utilizing the favored LangGraph framework. 

LangGraph for Brokers

LangGraph, constructed on prime of LangChain, is a framework that’s used to create and orchestrate AI Brokers utilizing stateful graphs (state is a shared knowledge construction used within the workflow). The graph consists of nodes, edges, and states. We’ll not delve into advanced workflows; we’ll create a easy workflow for this venture. LangGraph helps a number of LLM suppliers like OpenAI, Gemini, Anthropic, and many others. Be aware that I’ll be sticking to Gemini on this information. Instruments are an essential asset for Brokers that assist them lengthen their capabilities. What’s an AI Agent, you ask? AI Brokers are LLM-powered, which may cause or suppose to make selections and use instruments to finish the duty. Now let’s proceed to designing the move and coding the system. 

Workflow of the system

The objective of our system will likely be so as to add documentation strings for capabilities and flag any points as feedback within the code. Additionally, to make this method smarter, we’ll verify if the feedback exist already to skip including the identical utilizing the agentic system. Now, let’s take a look at the workflow I’ll be utilizing and delve into it. 

Flowchart

So, as you’ll be able to see, we’ve a analysis node which is able to use an agent that can take a look at the enter code and likewise “cause” if there’s documentation already current. It’ll then use conditional routing utilizing the ‘state’ to resolve the place to go subsequent. First, the documentation step writes the code based mostly on the context that the analysis node offers. Then, the evaluation node exams the code on a number of take a look at circumstances utilizing the shared context. Lastly, the final node saves the data in evaluation.txt and shops the documented code in code.py.

Coding the Agentic system

Pre-requisites

We are going to want the Gemini API Key to entry the Gemini fashions to energy the agentic system, and likewise the Tavily API Key for net search. Ensure you get your keys from the hyperlinks under:

Gemini: https://aistudio.google.com/apikey
Tavily: https://app.tavily.com/residence

For simpler use, I’ve added the repository to GitHub, which you’ll clone and use:

https://github.com/bv-mounish-reddy/Self-Documenting-Agentic-System.git

Make certain to create a .env file and add your API keys: 

GOOGLE_API_KEY=

TAVILY_API_KEY=

I used the gemini-2.5-flash all through the system (which is free to an extent) and used a few instruments to construct the system. 

Device Definitions

In LangGraph, we use the @instrument decorator to specify that the code/operate will likely be used as a instrument. We now have outlined these instruments within the code:

# Instruments Definition

@instrument

def search_library_info(library_name: str) -> str:

   """Seek for library documentation and utilization examples"""

   search_tool = TavilySearchResults(max_results=2)

   question = f"{library_name} python library documentation examples"

   outcomes = search_tool.invoke(question)

   formatted_results = []

   for end in outcomes:

       content material = outcome.get('content material', 'No content material')[:200]

       formatted_results.append(f"Supply: {outcome.get('url', 'N/A')}nContent: {content material}...")

   return "n---n".be a part of(formatted_results)

This instrument is utilized by the analysis agent to grasp the syntax related to the Python libraries used within the enter code and see examples of the way it’s getting used. 

@instrument

def execute_code(code: str) -> str:

   """Execute Python code and return outcomes"""

   python_tool = PythonREPLTool()

   attempt:

       outcome = python_tool.invoke(code)

       return f"Execution profitable:n{outcome}"

   besides Exception as e:

       return f"Execution failed:n{str(e)}"

This instrument executes the code with the inputs outlined by the evaluation agent to confirm whether or not the code works as anticipated and to verify for any loopholes.

Be aware: These capabilities are outlined utilizing the inbuilt LangGraph instruments: PythonREPLTool() and TavilySearchResults().

State Definition

The shared knowledge within the system must have a transparent construction to create a great workflow. I’m creating the construction as a TypedDict with the variables I’ll be utilizing within the agentic system. The variables will present context to the following nodes and likewise assist with the routing within the agentic system:

# Simplified State Definition

class CodeState(TypedDict):

   """Simplified state for the workflow"""

   original_code: str

   documented_code: str

   has_documentation: bool

   libraries_used: Listing[str]

   research_analysis: str

   test_results: Listing[str]

   issues_found: Listing[str]

   current_step: str

Agent Definitions

We used a ReAct (reasoning and performing) fashion agent for the ‘Analysis Agent’, which must study and cause. A ReAct-style agent can merely be outlined utilizing create_react_agent operate by passing the parameters, and this agent will likely be used within the node. Discover that we’re passing the beforehand outlined instrument within the create_react_agent operate. The node utilizing this Agent additionally updates among the state variables, which will likely be handed as context.

# Initialize Mannequin

def create_model():

   """Create the language mannequin"""

   return ChatGoogleGenerativeAI(

       mannequin="gemini-2.5-flash",

       temperature=0.3,

       google_api_key=os.environ["GOOGLE_API_KEY"]

   )

# Workflow Nodes

def research_node(state: CodeState) -> CodeState:

   """

   Analysis node: Perceive code and verify documentation

   Makes use of agent with search instrument for library analysis

   """

   print("RESEARCH: Analyzing code construction and documentation...")

   mannequin = create_model()

   research_agent = create_react_agent(

       mannequin=mannequin,

       instruments=[search_library_info],

       immediate=ChatPromptTemplate.from_messages([

           ("system", PROMPTS["research_prompt"]),

           ("placeholder", "{messages}")

       ])

   )

   # Analyze the code

   analysis_input = {

       "messages": [HumanMessage(content=f"Analyze this Python code:nn{state['original_code']}")]

   }

   outcome = research_agent.invoke(analysis_input)

   research_analysis = outcome["messages"][-1].content material

   # Extract libraries utilizing AST

   libraries = []

   attempt:

       tree = ast.parse(state['original_code'])

       for node in ast.stroll(tree):

           if isinstance(node, ast.Import):

               for alias in node.names:

                   libraries.append(alias.identify)

           elif isinstance(node, ast.ImportFrom):

               module = node.module or ""

               for alias in node.names:

                   libraries.append(f"{module}.{alias.identify}")

   besides:

       move

   # Verify if code has documentation

   has_docs = ('"""' in state['original_code'] or

               "'''" in state['original_code'] or

               '#' in state['original_code'])

   print(f"  - Libraries discovered: {libraries}")

   print(f"  - Documentation current: {has_docs}")

   return {

       **state,

       "libraries_used": libraries,

       "has_documentation": has_docs,

       "research_analysis": research_analysis,

       "current_step": "researched"

   }

Equally, we outline the opposite brokers as nicely for the nodes and tweak the prompts as wanted. We then proceed to outline the sides and workflow as nicely. Additionally, discover that has_documents variable is crucial for the conditional routing within the workflow.

Outputs

You’ll be able to change the code in the principle operate and take a look at the outcomes for your self. Right here’s a pattern of the identical:

Enter code

sample_code = """

import math

import random

def calculate_area(form, **kwargs):

   if form == "circle":

       return math.pi * kwargs["radius"] ** 2

   elif form == "rectangle":

       return kwargs["width"] * kwargs["height"]

   else:

       return 0

def divide_numbers(a, b):

   return a / b

def process_list(objects):

   whole = 0

   for i in vary(len(objects)):

       whole += objects[i] * 2

   return whole

class Calculator:

   def __init__(self):

       self.historical past = []

   def add(self, a, b):

       outcome = a + b

       self.historical past.append(f"{a} + {b} = {outcome}")

       return outcome

   def divide(self, a, b):

       return divide_numbers(a, b)

calc = Calculator()

outcome = calc.add(5, 3)

space = calculate_area("circle", radius=5)

division = calc.divide(10, 2)

objects = [1, 2, 3, 4]

processed = process_list(objects)

print(f"Outcomes: {outcome}, {space:.2f}, {division}, {processed}")

"""

Pattern Output

Documented Code
Example usage

Discover how the system says the random module is imported however not used. The system provides docstrings, flags points, and likewise provides feedback within the code about how the capabilities are getting used. 

Conclusion

We constructed a easy agentic system with using LangGraph and understood the significance of state, instruments, and brokers. The above system might be improved with using further nodes, instruments, and refinements within the prompts. This method might be prolonged to constructing a debugging system or a repository builder as nicely, with the best nodes and instruments. Additionally, keep in mind that utilizing a number of brokers can even end in increased prices when utilizing a paid mannequin, so create and use brokers that add worth to your agentic techniques and outline the workflows nicely prematurely. 

Incessantly Requested Questions

Q1. What’s LangGraph’s @instrument decorator?

A. It’s the way you mark a operate so a LangGraph agent can name it as a instrument inside workflows.

Q2. What’s ReAct in brokers?

A. It’s the loop the place brokers cause step-by-step, act with instruments, then observe outcomes.

Q3. Can this lengthen to real-world apps?

A. Yeah, you’ll be able to plug it into audits, debugging, compliance, and even reside information bases. Code documentation is likely one of the use circumstances.

Keen about know-how and innovation, a graduate of Vellore Institute of Expertise. At present working as a Information Science Trainee, specializing in Information Science. Deeply excited by Deep Studying and Generative AI, wanting to discover cutting-edge strategies to unravel advanced issues and create impactful options.

Login to proceed studying and luxuriate in expert-curated content material.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles