I am trying to create a shader that renders visible when looking at it from behind another invisible object, and vice versa. However, when the object is supposed to be invisible, it renders a black artifact in it's place. Is there any way to get rid of this and simply keep it transparent?
I believe the issue you're having is due to a circular dependency. You're counting the number of Transparent Shaders the ray is passing through, but what happens when the material that that number depends on is a Transparency itself? I believe that's why Blender creates a Null shader.
You can do something similar with a Glass Transmission Shader set to IOR of 1.0 for your "transparent window" thingy. Disable everything but Camera in the Cycles Setting:
Then instead of Transparent Depth, just use the Transmission Depth input for your object: